4.最后的代码为
import os
import json
#获取指定目录下所有的文件
dir = 'all'
all_files = [f for f in os.listdir(dir) if os.path.isfile(os.path.join(dir, f))]
for file in all_files:
#将所有扩展名不是.txt的文件改名为同名.txt文件
if not file.endswith('.txt'):
os.rename(os.path.join(dir, file), os.path.join(dir, os.path.splitext(file)[0] + '.txt'))
file = os.path.splitext(file)[0] + '.txt'
#对于每个txt文件,删除前9行的内容并保存到新的txt文件中
with open(os.path.join(dir, file), 'r', encoding='utf-8') as txt_file:
content = txt_file.readlines()
deleted_content = '\n'.join(content[:9])
new_content = ''.join(content[9:])
with open(os.path.join(dir, file), 'w', encoding='utf-8') as txt_file:
txt_file.write(new_content)
with open(os.path.join(dir, json_file), 'r', encoding='utf-8') as j_file:
data = json.load(j_file)
columns = list(data['page']['list'][0].keys())
rows = []
for item in data['page']['list']:
row_values = []
for column in columns:
value = str(item[column]).replace('\n','').replace(',','')
row_values.append(value)
rows.append(','.join(row_values))
#整理json文件中的数据,并按照列名的顺序写入数据文件out.txt中
with open('out.txt', 'a+', encoding='utf-8') as out_file:
if out_file.tell() == 0:
out_file.write(','.join(columns) + '\n')
out_file.write('\n'.join(rows)+'\n')