【问题标题】:How to read a text file then convert it to a list of tuples如何读取文本文件然后将其转换为元组列表
【发布时间】:2019-04-13 14:03:13
【问题描述】:

我要转换的文本文件包含例如:

Alex
Gheith
40
John
Stewart
33

进入:

[('Alex','Gheith','40'),('John','Stewart','33')]

当前代码:

records =[]
f10 = open("PlayerRecords.txt","r")
for line in f10:
    line = line.strip()
    records.append(line)
t = ()
f10.close()
t = [(x,) for x in records]
print t

当前输出:

[('Alex','Gheith',40),('John','Stewart',33)]

【问题讨论】:

    标签: python-2.7 file tuples


    【解决方案1】:

    试试下面给出的方法。我已将 str1 作为多行字符串。

    list1 = [line.strip() for line in str1.splitlines()]
    l_iter = iter(list1)
    mapped = zip(l_iter,l_iter,l_iter)
    mapped = set(mapped)
    print (mapped)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-10
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多