从一个输入文件创建多个词典-Python
我有一个由这些数据组成的输出文件从一个输入文件创建多个词典-Python,python,dictionary,Python,Dictionary,我有一个由这些数据组成的输出文件 {'region_code': 'MO', 'postal_code': None, 'country_code': 'US', 'city': 'Lake Saint Louis'}, {'hash': 415446807, 'org': 'CompanyName', 'transport': 'tcp', 'data': 'HTTP/1.0 302 Found\r\nLocation'}, {'hash': 607324474, 'isp': 'ISP-N
{'region_code': 'MO', 'postal_code': None, 'country_code': 'US', 'city': 'Lake Saint Louis'}, {'hash': 415446807, 'org': 'CompanyName', 'transport': 'tcp', 'data': 'HTTP/1.0 302 Found\r\nLocation'}, {'hash': 607324474, 'isp': 'ISP-Name', 'transport': 'tcp', 'data': 'HTTP/1.1 500 Internal Server'}
我已经用
textToSearch = (", {'hash")
textToReplace = ("\n {'hash")
fileToSearch = ("filename.txt")
tempFile = open( fileToSearch, 'r+' )
for line in fileinput.input( fileToSearch ):
tempFile.write( line.replace( textToSearch, textToReplace ) )
tempFile.close()
#Not the simplest script but it works.
我收到了这封信,附加了原始文件:
{'region_code': 'MO', 'postal_code': None, 'country_code': 'US', 'city': 'Lake Saint Louis'}
{'hash': 415446807, 'org': 'CompanyName', 'transport': 'tcp', 'data': 'HTTP/1.0 302 Found\r\nLocation'}
{'hash': 607324474, 'isp': 'ISP-Name', 'transport': 'tcp', 'data': 'HTTP/1.1 500 Internal Server'}
现在,我试图让我的脚本通读附加的文件,将{}的每一行/集合视为一个新字典
file = open ("filename.txt" , "r")
for line in file :
thisdict =line
selection = thisdict["postal_code"]
print (selection)
我得到了这个错误:
Traceback (most recent call last):
File "C:\Users\XXXX\XXXX\Documents\XXDocuments\XXXX\FileExtractor.py", line 16, in <module>
print(thisdict[x])
TypeError: string indices must be integers
回溯(最近一次呼叫最后一次):
文件“C:\Users\XXXX\XXXX\Documents\XXDocuments\XXXX\FileExtractor.py”,第16行,在
打印(本目录[x])
TypeError:字符串索引必须是整数
我认为这与字典在文件上迭代并重新编写每个字典有关,或者将所有3行视为一个大字典
file = open ("filename.txt" , "r")
for line in file :
thisdict =line
selection = thisdict["postal_code"]
print (selection)
我怎样才能做到:
None
转换为null
[
,在文件末尾添加']`json
模块加载json。你可以很容易地浏览字典
执行步骤1、2、3后
[{"region_code": "MO", "postal_code": null, "country_code": "US", "city": "Lake Saint Louis"}, {"hash": 415446807, "org": "CompanyName", "transport": "tcp", "data": "HTTP/1.0 302 Found\r\nLocation"}, {"hash": 607324474, "isp": "ISP-Name", "transport": "tcp", "data": "HTTP/1.1 500 Internal Server"}]
现在您可以轻松地加载json文件
import json
data = json.load(open("data.json"))
for ind_dict in data:
print(ind_dict)
首先
None
转换为null
[
,在文件末尾添加']`json
模块加载json。你可以很容易地浏览字典
执行步骤1、2、3后
[{"region_code": "MO", "postal_code": null, "country_code": "US", "city": "Lake Saint Louis"}, {"hash": 415446807, "org": "CompanyName", "transport": "tcp", "data": "HTTP/1.0 302 Found\r\nLocation"}, {"hash": 607324474, "isp": "ISP-Name", "transport": "tcp", "data": "HTTP/1.1 500 Internal Server"}]
现在您可以轻松地加载json文件
import json
data = json.load(open("data.json"))
for ind_dict in data:
print(ind_dict)
事实上,我相信它这样做的原因是,当你从一个文件中读取时,你实际上是在读取字符串,所以你实际上读取的是具有以下格式的字符串: “{‘地区代码’:‘MO’,‘邮政编码’:无,‘国家代码’:‘美国’,‘城市’:‘圣路易斯湖’” 尝试使用json模块 虽然要实现这一点,附加文件中的qute必须是双引号,并且还要通过将其更改为“null”来处理全开的None值
事实上,我相信它这样做的原因是,当你从一个文件中读取时,你实际上是在读取字符串,所以你实际上读取的是具有以下格式的字符串: “{‘地区代码’:‘MO’,‘邮政编码’:无,‘国家代码’:‘美国’,‘城市’:‘圣路易斯湖’” 尝试使用json模块 虽然要实现这一点,附加文件中的qute必须是双引号,并且还要通过将其更改为“null”来处理全开的None值
确切地说,虽然你实际上不必添加“[”“]”,但如果没有方括号@tochibedford,它将无法工作。哦,我想这是因为我在上面的回答中做了不同的事情。确切地说,尽管你实际上不必添加“[”“]”如果没有方括号@tochibedford,它将无法工作。哦,我想这是因为我在上面的回答[line=line.replace(“\”,“\”)中做了不同的操作|这是表示替换换行符还是替换单引号?这是表示按照json模块的要求将单引号替换为双引号,否则会出现一个错误,提示您这样做[line=line.replace(“\”,“\”)]|这是指替换换行符还是替换单引号?这是指按照json模块的要求将单引号替换为双引号,否则会出现一个错误,提示您这样做