Python-处理txt文件中的json格式

单个json格式


'''
class.txt

{'PA':'乐','PE':'乐','PD':'好','PH':'好','PG':'好','PB':'好'}
'''

import os
import json
path_file = os.path.dirname(os.path.abspath(__file__))
class_path = os.path.join(path_file, "class.txt")

with open(class_path, "r", encoding="gbk") as fileopen:
     line_rd = fileopen.read()
     line_rd=line_rd.replace("'",'"')
     class_dict = json.loads(line_rd)
print(type(class_dict))
print(class_dict)

多个json格式


'''
dict1.txt


{"word":"脏乱","wordtype":"adj","wordcount":"1.0","wordnum":"1.0","news_mood_sec":"NN","strength":"7.0","polarity":"2.0","Auxiliary":"","Auxiliary_strength":"","Auxiliary_polarity":"","":"",}
{"word":"糟报","wordtype":"adj","wordcount":"1.0","wordnum":"1.0","news_mood_sec":"NN","strength":"5.0","polarity":"2.0","Auxiliary":"","Auxiliary_strength":"","Auxiliary_polarity":"","":"",}
{"word":"早衰","wordtype":"adj","wordcount":"1.0","wordnum":"1.0","news_mood_sec":"NE","strength":"5.0","polarity":"2.0","Auxiliary":"","Auxiliary_strength":"","Auxiliary_polarity":"","":"",}
'''

import os
import json
path_file = os.path.dirname(os.path.abspath(__file__))
key_path = os.path.join(path_file, "dict1.txt")

list_words = []
list_all = []
with open(key_path, "r",encoding='utf-8') as fileopen1:
    for line in fileopen1.readlines():
        #这里因为每个json最后一个大括号前面需要删除逗号,但是数量比较多,就懒得删除了,不这样写,通过不了
        line_new=line[:-3]+'}'
        data = json.loads(line_new)
        word=data['word']
        news_mood_sec=data['news_mood_sec']
        strength=data['strength']
        polarity = data['polarity']
        list_words.append(word)
        list_all.append([news_mood_sec,strength,polarity])
print(list_words)
print(list_all)

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来为你讲解如何使用Pythontxt文件批量转为JSON。 假设你有多个txt文件,它们的文件名都以.txt结尾,你想将它们批量转换为JSON格式保存在同一目录下。具体的步骤如下: 1. 导入必要的模块 首先,需要导入os和json模块。os模块提供了访问操作系统功能的接口,json模块提供了处理JSON格式的函数。 ``` import os import json ``` 2. 遍历txt文件 使用os模块的listdir函数遍历存储txt文件的目录。然后,使用Python的for循环来迭代目录的每个文件,找到以.txt结尾的文件,并对它们进行操作。在循环,使用os.path.join函数创建文件的完整路径。 ``` txt_folder = "/path/to/txt/folder" # 存储txt文件的目录 json_folder = "/path/to/json/folder" # 存储json文件的目录 for file_name in os.listdir(txt_folder): if file_name.endswith(".txt"): txt_path = os.path.join(txt_folder, file_name) ``` 3. 读取txt文件并转换为JSON格式 对于每个txt文件,使用Python的with语句打开文件并读取其内容。然后,使用json模块的loads函数将其转换为Python的字典类型。 ``` with open(txt_path, 'r') as file: text = file.read() data = json.loads(text) ``` 4. 将Python字典转换为JSON格式 使用json模块的dumps函数将Python字典转换为JSON格式。 ``` json_data = json.dumps(data) ``` 5. 保存JSON文件JSON数据写入名为file_name.json文件,并将其保存json_folder。可以使用Python内置的open函数打开一个新文件,并使用write函数将JSON数据写入该文件。 ``` json_path = os.path.join(json_folder, file_name.replace(".txt", ".json")) with open(json_path, 'w') as file: file.write(json_data) ``` 最终的完整代码如下: ``` import os import json txt_folder = "/path/to/txt/folder" # 存储txt文件的目录 json_folder = "/path/to/json/folder" # 存储json文件的目录 for file_name in os.listdir(txt_folder): if file_name.endswith(".txt"): txt_path = os.path.join(txt_folder, file_name) with open(txt_path, 'r') as file: text = file.read() data = json.loads(text) json_data = json.dumps(data) json_path = os.path.join(json_folder, file_name.replace(".txt", ".json")) with open(json_path, 'w') as file: file.write(json_data) ``` 希望这个代码可以帮助到你。如果还有其他问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值