【问题标题】:how can i write a list of tuples (include unicode) on a txt file?如何在 txt 文件中编写元组列表(包括 unicode)?
【发布时间】:2015-12-06 17:47:03
【问题描述】:

我有一个清单:

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 文件中逐行写入该列表?,这样:

('计算机', 1592)

('学生', 1113)

('大学', 1080)

... ... ...

我怎样才能写出这样的字典

d = {u'computer':1592 , u'student':1113,........}

【问题讨论】:

  • 你读过文件 I/O ...docs.python.org/2.7/tutorial/…
  • 您需要更具体的格式。元组有引号但没有 u?这些东西在编写时应该看起来像 python 数据结构吗?
  • @Nicqu,写成元组是没有意义的,除非你可能会腌制数据,按照我的答案的第一部分使用 csv 模块,或者如果你要使用 dict 那么只需创建一个从数据中提取并用 json 转储。

标签: python list unicode tuples


【解决方案1】:

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

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

这是输出:

('计算机', 1592)

('学生', 1113)

('大学', 1080)

('覆盆子', 1000)

('科学', 814)

('$5', 770)

('pi', 688)

('考试', 571)

('刚刚', 544)

('智能', 495)

('解决方案', 475)

(“成本”,423)

('考试:', 411)

('最新的', 402)

('pi's', 366)

('是', 311)

('可以', 268)

('什么', 268)

('方式', 257)

('学生', 238)

【讨论】:

  • 更好用with open('Output.txt', 'w') as file_out:
  • 当我使用它时,我遇到了这样的错误 TypeError: expected a character buffer object
  • 我再次遇到这样的错误 k.write(str(element)) TypeError: 'unicode' object is not callable
  • 我已经编辑了代码,请再试一次,因为它在我的系统上运行良好。
  • 再次出现 TypeError: 'unicode' object is not callable, 你是否在每个字符串前面添加 u 例如 mylist=[(u'computer', 1592),......]跨度>
【解决方案2】:

如果您真的希望每行一对并单独创建一个字典,请使用 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

要创建字典,只需在 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}

【讨论】:

  • @Nicqu,如果你想要一个 dict 并计划作为 dict 访问,那么存储为 dict 是有意义的,将元组表示形式写成字符串对你来说绝对没有用
【解决方案3】:

由于您使用的是 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}

【讨论】:

  • 或者忘记写为 str 并只写 unicode,添加所有工作只是为了存储在您想要使用时需要更多解析的格式是没有意义的又来了
  • 我很感谢你:))))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-23
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多