如何使用 python 从 docx 读取 json 文件
How to read json file from docx using python
我有一个如下所示的 docx 文件。
[ { "name": "ravi",
"city": "chennai",
"country": "india"
}
{
"name": "raj",
"city": "chennai",
"country": "india"
}
]
我正在尝试将此内容作为字典列表来阅读。我试过了。
from docx import Document
document = Document('2255858_1321163255_worldcupdata.docx')
s=""
for i in document.paragraphs:
s+=i.text
json_list=s[1:-1].split('}')
但我无法将字符串转换为字典,我收到以下语句的值错误。
dict('{ "name": "ravi", "city": "chennai", "country": "india" }')
ValueError: dictionary update sequence element #0 has length 1; 2 is required
您可以使用json
[编辑]
from docx import Document
import regex as re
import json
document = Document('2255858_1321163255_worldcupdata.docx')
s=""
for i in document.paragraphs:
s+=i.text
p = re.compile('\[(.*?)\]')
d = json.loads(p.search(s).group(0))
做d = json.loads(your_string)
d 将是你的字典
loads
来自 json 将字符串转换为字典
我有一个如下所示的 docx 文件。
[ { "name": "ravi",
"city": "chennai",
"country": "india"
}
{
"name": "raj",
"city": "chennai",
"country": "india"
}
]
我正在尝试将此内容作为字典列表来阅读。我试过了。
from docx import Document
document = Document('2255858_1321163255_worldcupdata.docx')
s=""
for i in document.paragraphs:
s+=i.text
json_list=s[1:-1].split('}')
但我无法将字符串转换为字典,我收到以下语句的值错误。
dict('{ "name": "ravi", "city": "chennai", "country": "india" }')
ValueError: dictionary update sequence element #0 has length 1; 2 is required
您可以使用json
[编辑]
from docx import Document
import regex as re
import json
document = Document('2255858_1321163255_worldcupdata.docx')
s=""
for i in document.paragraphs:
s+=i.text
p = re.compile('\[(.*?)\]')
d = json.loads(p.search(s).group(0))
做d = json.loads(your_string)
d 将是你的字典
loads
来自 json 将字符串转换为字典