I’m loading the following JSON into my program:
JavaScript
x
21
21
1
d =
2
[
3
{
4
"extraction_method": "lattice",
5
"top": 18.0,
6
"left": 18.0,
7
"width": 575.682861328125,
8
"height": 756.0,
9
"right": 593.68286,
10
"bottom": 774.0,
11
"data": [
12
[
13
{
14
"top": 108.0,
15
"left": 18.0,
16
"width": 575.682861328125,
17
"height": 53.67376708984375,
18
"text": "apple foo textrhello world"
19
},
20
21
I want to extract the substring “apple foo text” with:
JavaScript
1
2
1
print(d[0][0]['data'][0][0]['text'])
2
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.
Advertisement
Answer
To navigate to string, you’re using:
JavaScript
1
2
1
string = d[0][0]['data'][0][0]['text']
2
To get the desired substring split text on carriage return.
JavaScript
1
3
1
substring = string.split('r')[0]
2
print(substring) # Result is apple foo text
3