1:txt文件转换成Excel中的xlsx文件
file_path = ''
excel_path = ''
#
with open(file_path, "r", encoding="utf-8", errors="replace") as file:
dedata = {'列名1': [], '列名2': []}
for line in file:
line = line.strip() # 移除行末尾的换行符和空白字符
data = line.split()
data_list = eval(data[0])
for item in data_list:
sentence = item[0]
number= item[1]
dedata['列名1'].append(sentence)
dedata['列名2'].append(number)
df = pd.DataFrame(dedata)
# 将数据导入到Excel文件中
df.to_excel(excel_path, index=False)
2:JSONl文件的读取与写入
import json
input_file = ''
output_file = ''
# 逐行读取JSON文件,并保存为JSONL文件
total = 0
# 打开原始JSON文件和目标文件
with open(input_file, 'r',encoding='utf-8') as file_in, open(output_file, 'a',encoding='utf-8') as file_out:
for line in file_in:
try:
data = json.loads(line)
json.dump(data, file_out,ensure_ascii=False)
file_out.write('\n') # 写入换行符,保持每行一个JSON对象
file_out.flush()
except json.JSONDecodeError:
print("文件内容不符合JSON格式")
3:txt文件的读取与写入
file_path = ''
file_t = ''
with open(file_path, "r", encoding="utf-8", errors="replace") as file, open(file_t, "a+", encoding="utf8") as ft:
# 逐行读取文件内容
for line in file:
line = line.strip() # 移除行末尾的换行符和空白字符
ft.write(line + '\n')
4:JSON文件转换成Excel文件
"""
json 文件输出成Excel
"""
import json
import json
import pandas as pd
json_path = ''
excel_path = ''
# 读取JSON文件并按行加载数据
data = []
with open(json_path, "r",encoding='utf-8') as json_file:
for line in json_file:
data.append(json.loads(line))
# 将数据转换为DataFrame
df = pd.DataFrame(data)
# 保存DataFrame到Excel文件
df.to_excel(excel_path, index=False)
5:JSON To JSON
import json
import pandas as pd
# 读取JSON文件并按行加载数据
yuanshidata = ''
newdata = ''
with open(yuanshidata, "r",encoding='utf-8') as json_file,open(newdata, "a+", encoding="utf-8") as ft:
for line in json_file:
data = json.loads(line) #将 JSON 字符串解析为 Python 对象。
jsondata = {
"""需要的键值对
"""
}
json_str = json.dumps(jsondata, ensure_ascii=False) #将 Python 对象转换为 JSON 格式的字符串。
ft.write(json_str + '\n')
一些常用数据类型的转换