我从蔚蓝数据湖中下载了以下格式的文件:
{"PartitionKey":"2020-10-05","value":"Resolved"...}
{"PartitionKey":"2020-10-06","value":"Resolved"...}
我只想在python中读取和解析此内容。
def read_ods_file():
file_path = 'temp.json'
data = []
with open(file_path) as f:
for line in f:
data.append(json.loads(line))
这给了我一个例外:
data.append(json.loads(line))
File "C:\python3.6\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\python3.6\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\python3.6\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
打印行会在开始时显示这些添加的字符。这些添加的字符是什么?
{"PartitionKey":"2020-10-05","value":"Resolved"...}
{"PartitionKey":"2020-10-06","value":"Resolved"...}
答案 0 :(得分:1)
Microsoft使用各种奇怪的字符。您可以尝试使用string.printable
仅获得如下所示的普通ASCII字符:
How can I remove non-ASCII characters but leave periods and spaces using Python?
答案 1 :(得分:0)
您设置的f
变量
with open(file_path) as f:
是python文件对象(类型为_io.TextIOWrapper
)。
如果您想将每行作为json对象读取,则应尝试以下操作:
with open(file_path) as f:
# read the file contents into a string
# strip off trailing whitespace
# split string into list of strings on \n character
for line in f.read().strip().splitlines():
data.append(json.loads(line))