Python中的一些格式转换 txt json xlsx等格式转换方式

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')



一些常用数据类型的转换

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值