从 json 文件中提取文本并保存到文本文件中
Extracting text from json file and saving into text file
import json
file= open('webtext.txt','a+')
with open('output-dataset_v1_webtext.test.jsonl') as json_file:
data= json.load(json_file)
for item in data:
file.write(item)
print(item)
>>> I am getting this error:
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 656)
I have already tried with json.loads()
My json file look like with multiple objects:
{"id": 255000, "ended": true, "length": 134, "text": "Is this restaurant fami"}
{"id": 255001, "ended": true, "length": 713, "text": "Clinton talks about her time of 'refle"}
任何关于如何解决现有问题并将dict['text']
写入文本文件
的建议将不胜感激
我当然不是 JSON 专家,因此可能有更好的方法来执行此操作,但您应该能够通过将顶级数据放入数组来解决您的问题:
[
{"id": 255000, "ended": true, "length": 134, "text": "Is this restaurant fami"},
{"id": 255001, "ended": true, "length": 713, "text": "Clinton talks about her time of 'refle"}
]
您遇到的错误基本上是在告诉您,可能只有一个顶级 JSON 实体。如果你想要更多,它们必须放在一个数组中。
看来您需要遍历文件中的每一行,然后使用 json.loads
。
例如:
with open('output-dataset_v1_webtext.test.jsonl') as json_file:
for line in json_file: #Iterate Each Line
data= json.loads(line.strip()) #Use json.loads
for item in data:
file.write(item)
print(item)
你需要遍历它:
import json
with open('output-dataset_v1_webtext.test.jsonl','r') as json_file:
for line in json_file.readlines():
data= json.loads(line)
for item in data:
print(item)
正如其他人所指出的,您的 JSON 必须用方括号括起来,因为它只能有一个顶级对象。
比如像这样:
[
{"id": 255000,"ended": true, "length": 134, "text": "Is this restaurant fami"},
{"id": 255001, "ended": true, "length": 713, "text": "Clinton talks about her time of 'refle"}
]
那么,您应该能够使用此代码来执行您正在尝试的操作:
import json
file = open('webtext.txt', 'a')
with open('test.json') as json_file:
data = json.load(json_file)
for item in data:
file.write(str(item))
print(item)
为了解决您的 file.write
问题,您需要将 item
转换为字符串,如下所示:str(item)
.
import json
file= open('webtext.txt','a+')
with open('output-dataset_v1_webtext.test.jsonl') as json_file:
data= json.load(json_file)
for item in data:
file.write(item)
print(item)
>>> I am getting this error:
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 656)
I have already tried with
json.loads()
My json file look like with multiple objects:
{"id": 255000, "ended": true, "length": 134, "text": "Is this restaurant fami"}
{"id": 255001, "ended": true, "length": 713, "text": "Clinton talks about her time of 'refle"}
任何关于如何解决现有问题并将dict['text']
写入文本文件
我当然不是 JSON 专家,因此可能有更好的方法来执行此操作,但您应该能够通过将顶级数据放入数组来解决您的问题:
[
{"id": 255000, "ended": true, "length": 134, "text": "Is this restaurant fami"},
{"id": 255001, "ended": true, "length": 713, "text": "Clinton talks about her time of 'refle"}
]
您遇到的错误基本上是在告诉您,可能只有一个顶级 JSON 实体。如果你想要更多,它们必须放在一个数组中。
看来您需要遍历文件中的每一行,然后使用 json.loads
。
例如:
with open('output-dataset_v1_webtext.test.jsonl') as json_file:
for line in json_file: #Iterate Each Line
data= json.loads(line.strip()) #Use json.loads
for item in data:
file.write(item)
print(item)
你需要遍历它:
import json
with open('output-dataset_v1_webtext.test.jsonl','r') as json_file:
for line in json_file.readlines():
data= json.loads(line)
for item in data:
print(item)
正如其他人所指出的,您的 JSON 必须用方括号括起来,因为它只能有一个顶级对象。 比如像这样:
[
{"id": 255000,"ended": true, "length": 134, "text": "Is this restaurant fami"},
{"id": 255001, "ended": true, "length": 713, "text": "Clinton talks about her time of 'refle"}
]
那么,您应该能够使用此代码来执行您正在尝试的操作:
import json
file = open('webtext.txt', 'a')
with open('test.json') as json_file:
data = json.load(json_file)
for item in data:
file.write(str(item))
print(item)
为了解决您的 file.write
问题,您需要将 item
转换为字符串,如下所示:str(item)
.