如何在回车之前获取文本 return

How to get text before carriage return

我正在将以下 JSON 加载到我的程序中:

d =
[
  {
    "extraction_method": "lattice",
    "top": 18.0,
    "left": 18.0,
    "width": 575.682861328125,
    "height": 756.0,
    "right": 593.68286,
    "bottom": 774.0,
    "data": [
      [
        {
          "top": 108.0,
          "left": 18.0,
          "width": 575.682861328125,
          "height": 53.67376708984375,
          "text": "apple foo text\rhello world"
        },
...

我想提取子字符串“apple foo text”:

print(d[0][0]['data'][0][0]['text'])

但它只有 returns hello world。我知道这是因为回车 return 语句,但我不确定之前如何打印子字符串。我怎样才能得到声明之前的文字?任何帮助将不胜感激。

repr呢。已经提到here

Help on built-in function repr in module builtins:

repr(obj, /)
    Return the canonical string representation of the object.
    
    For many object types, including most builtins, eval(repr(obj)) == obj.
print(repr(d[0][0]['data'][0][0]['text']))

输出

'apple foo text\rhello world'

要导航到字符串,您正在使用:

string = d[0][0]['data'][0][0]['text']

在回车 return.

上获取所需的子字符串拆分文本
substring = string.split('\r')[0]
print(substring) # Result is apple foo text