读取json文件并遍历
import json
json_file = '/xxx/xxxx.json'
with open(json_file,'r',encoding="utf8") as file_obj:
dataset = json.load(file_obj)
for data in dataset:
print(data['Index'])
list保存为json文件
import json
total_json=[
{
'Index':0,
'content':'xxxx'
},
{
'Index':1,
'content':'xxxx'
}
...
]
save_json_path = '/xxx/xxxx.json'
with open(save_json_path,'w',encoding='utf-8') as file_obj:
json.dump(total_json,file_obj,ensure_ascii=False,indent=4)
按行读取txt文件
假如txt文件内容如下:
代码:
filename = '/xxx/xxxx.txt'
with open(filename) as f:
ids = f.readlines()
ids = [i.strip() for i in ids]