让我们说我有一个文件,该文件的每一行和每一行我都希望在字典中进行转换。在字典中将有一个字符串,两个整数和两个列表。有点像这样: Q1 = {“ string”:“ name”, “ integer1”:1 “ integer2”:2 “ list1”:[a,b,c,d], “ list2”:[] } 现在该文件可以是txt文件,也可以不是。可以说它是一个txt文件,每一行都会提供一个字典。我是一个编写文件的人,因此我可以使用任何想要的格式。我认为file.txt的每一行都采用这种格式: 名称,1,2,(a / b / c),() 所以第一件事是字符串,第二件事是integer1,然后是integer2,list1和list2。字典的每个元素都用逗号分隔,括号内的列表元素则用斜杠(“ /”)分隔。显然,如果您认为file.txt的格式更好,请告诉我:) 我希望它是动态的第一个列表。这意味着有些行可能在圆括号()内包含更多字符,第二个列表我希望它始终为空,因为我稍后将这些内容放在代码中。 每行是一个字典,每行字典是我要创建的列表中的一个元素。所以我要列出字典。
我试图打开一个文件并使用split功能播放,但是txt文件的格式比我想象的要复杂得多,从来没有读取过该文件并将其保存到词典列表中
#the file.txt should look like this:
name1,5,6,(a1/a2/a3), ()
name2,7,8,(a2/a3/a4/a5), ()
#the python code i tried:
def init():
myList=[]
with open("file.txt") as f:
for line in f:
d={}
d = dict(line.strip().split(',', 4))
myList[line]=d
return(myList)
list=[]
list=init();
答案 0 :(得分:2)
请检查此...,不使用任何包装。
with open("test.txt") as f:
lines = [ line.strip().split(",") for line in f ]
lines = [{
"string": line[0],
"integer1": int(line[1]),
"integer2": int(line[2]),
"list1": [l for l in line[3].strip("()").split("/")],
"list2": [l for l in line[4].strip("()").split("/")],
} for line in lines ]
print(lines)
答案 1 :(得分:1)
在file1.txt内部
hey1,5,6,(a1 / a2 / a3),()
hey2,7,8,(a2 / a3 / a4 / a5),()
您可以使用下面的代码,它将为每一行生成一个新字典,并最终在dict_main内生成所有字典。
index1=['string1','integer1','integer2','list1','list2']
dict_main={}
with open ('file1.txt') as f:
count=0
for line in f:
dict1={}
lst1=line.strip().split(',')
dict1[index1[0]]=lst1[0]
dict1[index1[1]]=int(lst1[1])
dict1[index1[2]]=int(lst1[2])
dict1[index1[3]]=lst1[3][1:-1].strip().split('/')
dict1[index1[4]]=[]
count+=1
dict_main['dict'+str(count)]=dict1
print(dict_main)
结果
{'dict1': {'integer2': 6, 'list2': [], 'integer1': 5, 'list1': ['a1', 'a2', 'a3'], 'string1': 'hey1'}, 'dict2': {'integer2': 8, 'list2': [], 'integer1': 7, 'list1': ['a2', 'a3', 'a4', 'a5'], 'string1': 'hey2'}}
答案 2 :(得分:0)
您可以为此使用csv.DictReader
。
使用给定的示例文件,您可以像这样使用它:
from csv import DictReader
FIELD_NAMES = ["string", "integer1", "integer2", "list1", "list2"]
with open("file_name.csv") as f:
reader = DictReader(f, fieldnames=FIELD_NAMES)
for line in reader:
# line["integer1"] = int(line["integer1"])
# ...
print(line)
# OrderedDict([('string', 'name1'), ('integer1', '5'), ('integer2', '6'), ('list1', '(a1/a2/a3)'), ('list2', ' ()')])
# OrderedDict([('string', 'name2'), ('integer1', '7'), ('integer2', '8'), ('list1', '(a2/a3/a4/a5)'), ('list2', ' ()')])
如您所见,它会将每个字段都评估为一个字符串,因此您必须将解析添加到整数和列表中,但这应该可以帮助您入门。
它也返回OrderedDict
,以确保字段顺序。如果需要,可以使用dict()
将它们转换为普通字典。
要获取字典列表,只需执行以下操作:
with open("file_name.csv") as f:
reader = DictReader(f, fieldnames=FIELD_NAMES)
print(list(reader))
# [OrderedDict([('string', 'name1'), ('integer1', '5'), ('integer2', '6'), ('list1', '(a1/a2/a3)'), ('list2', ' ()')]), OrderedDict([('string', 'name2'), ('integer1', '7'), ('integer2', '8'), ('list1', '(a2/a3/a4/a5)'), ('list2', ' ()')])]
略有关联:
list
来隐藏内置list
return
不需要用括号括起来,return
后的空格就足够了。答案 3 :(得分:0)
您还可以使用正则表达式,并根据需要避免使用“ /”字符,例如:
import re
txt1 = "name1,5,6,[a1,a2,a3],[]"
regex = "([a-zA-Z0-9]*),([0-9]+),([0-9]+),\[(.*)\],\[(.*)\]"
matches = re.match(regex, txt1)
dict1 = {"string": matches.group(1), "integer1": matches.group(2), "integer2": matches.group(3),
"list1": matches.group(4).split(","), "list2": matches.group(5).split(",")}
结果是:
{'string': 'name1', 'integer1': '5', 'integer2': '6', 'list1': ['a1', 'a2', 'a3'], 'list2': ['']}