0

I'm loading the following JSON into my program:

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"
        },
...

I want to extract the substring "apple foo text" with:

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

But it only returns hello world. I know it is because of the carriage return statement, but I'm not sure how to to print the substring before. How would I get just the text before the statement? Any help would be appreciated.

mehsheenman
  • 522
  • 4
  • 11
  • Does this answer your question? [Python print string like a raw string](https://stackoverflow.com/questions/26520111/python-print-string-like-a-raw-string) – Epsi95 Jan 23 '21 at 10:36

2 Answers2

1

What about repr. Already mentioned 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']))

output

'apple foo text\rhello world'
Epsi95
  • 8,832
  • 1
  • 16
  • 34
1

To navigate to string, you're using:

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

To get the desired substring split text on carriage return.

substring = string.split('\r')[0]
print(substring) # Result is apple foo text
DarrylG
  • 16,732
  • 2
  • 17
  • 23