[英]How to get text before carriage return
I'm loading the following JSON into my program:我正在将以下 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"
},
...
I want to extract the substring "apple foo text" with:我想用以下方法提取 substring “apple foo text”:
print(d[0][0]['data'][0][0]['text'])
But it only returns hello world
.但它只返回
hello world
。 I know it is because of the carriage return statement, but I'm not sure how to to print the substring before.我知道这是因为回车语句,但我不确定如何打印 substring 之前。 How would I get just the text before the statement?
我怎样才能得到声明之前的文字? Any help would be appreciated.
任何帮助,将不胜感激。
What about repr
.怎么样
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 output
'apple foo text\rhello world'
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 拆分文本。
substring = string.split('\r')[0]
print(substring) # Result is apple foo text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.