【发布时间】:2019-12-07 06:00:20
【问题描述】:
尝试创建与 XML 文件同名的 JSON 文件并使用 Python 将 XML 数据转储到该文件中
import os
import json
import xmltodict
# Reading file from directory
with os.scandir('C:/jsonfile/') as entries:
for entry in entries:
name=(entry.name)
print(name)
base = os.path.splitext(name)[0] #Getting name of the file
f= open("C:/jjsonfile/"+base+".json","w+")
with open("C:/jsonfile/"+name, 'r') as f: #Creating JSON file
xmlString = f.read()
jsonString = json.dumps(xmltodict.parse(xmlString), indent=4)
with open(f, 'w') as f: #Loading data into JSON file.
f.write(jsonString)
1019586313.xml
----------------------------------- ---------------------------- TypeError Traceback(最近一次调用 最后)在 13 xmlString = f.read() 14 jsonString = json.dumps(xmltodict.parse(xmlString), 缩进=4) ---> 15 with open(f, 'w') as f: 16 f.write(jsonString) 17
TypeError: 预期的 str、bytes 或 os.PathLike 对象,不是 _io.TextIOWrapper
【问题讨论】:
-
尝试做 json.dumps(xmltodict.parse(str(xmlString)), indent=4)
-
不..!! “not _io.TextIOWrapper”的主要问题
标签: python python-3.x python-2.7