我有一个清单:
mylist=[(u'computer', 1592), (u'student', 1113), (u'university', 1080), (u'raspberry', 1000), (u'science', 814), (u'$5', 770), (u'pi', 688), (u'exam', 571), (u'just', 544), (u'intelligence', 495), (u'solution', 475), (u'costs', 423), (u'exam:', 411), (u'latest', 402), (u"pi's", 366), (u'be', 311), (u'can', 268), (u'what', 268), (u'way', 257), (u'students', 238)]
如何逐行在txt
文件上编写该列表?这样:
('computer',1592)
('学生',1113)
('university',1080)
...... ......
我怎么能写这样的字典
d = {u'computer':1592 , u'student':1113,........}
答案 0 :(得分:2)
mylist = [(u' computer',1592),(u' student',1113),(u' university',1080),(u' raspberry',1000),(u' science',814),(u' $ 5',770),(u' pi',688),(u& #39;考试,#571),(你'只是',544),(你'情报'),(你'解决方案',475), (u'费用',423),(u'考试:',411),(u'最新',402),(你" pi' s",366),(你',311),(你可以',268),(你'什么',268),(你&# 39;方式',257),(你'学生',238)]
text_file = open(" Output.txt"," w")
for element in mylist:
text_file.write("('" + str(element[0]) + "', " + str(element[1]) + ")")
text_file.write("\n")
text_file.close()
这是输出:
(' computer',1592)
('学生',1113)
(' university',1080)
(' raspberry',1000)
(' science',814)
(' $ 5',770)
(' pi',688)
('考试',571)
(' just',544)
('情报')
('解决方案',475)
('费用',423)
('考试:',411)
('最新',402)
(' pi',366)
(' be',311)
(' can',268)
(' what',268)
(' way',257)
('学生',238)
答案 1 :(得分:1)
如果你真的希望每行一对并分别创建一个dict,请使用csv模块并忘记元组:
import csv
with open("tup.txt","w") as f:
csv.writer(f).writerows(mylist)
输出:
computer,1592
student,1113
university,1080
raspberry,1000
science,814
$5,770
pi,688
exam,571
just,544
intelligence,495
solution,475
costs,423
exam:,411
latest,402
pi's,366
be,311
can,268
what,268
way,257
students,238
并创建dict只需在myList上调用dict:
d = dict(myList)
输出:
{u'be': 311, u'what': 268, u'$5': 770, u'exam': 571, u'just': 544, u'students': 238, u'science': 814, u'university': 1080, u'way': 257, u'solution': 475, u'costs': 423, u"pi's": 366, u'computer': 1592, u'can': 268, u'student': 1113, u'intelligence': 495, u'pi': 688, u'raspberry': 1000, u'exam:': 411, u'latest': 402}
如果你基本上想要一个字典,那么你可以使用json创建并转储一个字典:
import json
with open("data.json","w") as f:
json.dump(dict(mylist), f)
将您的数据存储为:
{"what": 268, "science": 814, "pi's": 366, "can": 268, "be": 311, "exam:": 411, "university": 1080, "costs": 423, "intelligence": 495, "latest": 402, "just": 544, "solution": 475, "$5": 770, "raspberry": 1000, "student": 1113, "way": 257, "computer": 1592, "exam": 571, "students": 238, "pi": 688}
然后再次加载:
with open("data.json") as f:
d = json.load(f)
print(d)
这将再次给你一个字典:
{'raspberry': 1000, 'exam': 571, 'what': 268, 'be': 311, 'intelligence': 495, 'latest': 402, 'computer': 1592, 'university': 1080, '$5': 770, 'science': 814, 'can': 268, 'costs': 423, 'students': 238, 'solution': 475, 'student': 1113, 'pi': 688, 'exam:': 411, 'just': 544, 'way': 257, "pi's": 366}
答案 2 :(得分:0)
由于您正在使用unicode数据,因此需要确定文件的编码,因为非ascii字符无法本机写入文件。 “utf-8”是最广泛接受的格式,所以我将继续这样做。您的dict
看起来只是想要dict
的python表示,但是您的列表有点不同(前面的“u”缺失)。所以,我做的有点不同了。
此脚本
mylist=[(u'computer', 1592), (u'student', 1113), (u'university', 1080), (u'raspberry', 1000), (u'science', 814), (u'$5', 770), (u'pi', 688), (u'exam', 571), (u'just', 544), (u'intelligence', 495), (u'solution', 475), (u'costs', 423), (u'exam:', 411), (u'latest', 402), (u"pi's", 366), (u'be', 311), (u'can', 268), (u'what', 268), (u'way', 257), (u'students', 238)]
with open('test1.txt', 'w') as fp:
# write line by line without python 2 style string encoding marker
for item in mylist:
fp.write(u"('{}', {})\n".format(*item).encode('utf-8'))
fp.write('\n')
# write python 2 representation of a dict
mydict = dict(mylist)
fp.write(repr(mydict).encode('utf-8'))
fp.write('\n')
print open('test1.txt').read().decode('utf-8')
产生这些结果
('computer', 1592)
('student', 1113)
('university', 1080)
('raspberry', 1000)
('science', 814)
('$5', 770)
('pi', 688)
('exam', 571)
('just', 544)
('intelligence', 495)
('solution', 475)
('costs', 423)
('exam:', 411)
('latest', 402)
('pi's', 366)
('be', 311)
('can', 268)
('what', 268)
('way', 257)
('students', 238)
{u'be': 311, u'what': 268, u'$5': 770, u'exam': 571, u'just': 544, u'students': 238, u'science': 814, u'university': 1080, u'way': 257, u'solution': 475, u'costs': 423, u"pi's": 366, u'computer': 1592, u'can': 268, u'student': 1113, u'intelligence': 495, u'pi': 688, u'raspberry': 1000, u'exam:': 411, u'latest': 402}