Python将文本文件作为列表导入以进行迭代

Python将文本文件作为列表导入以进行迭代,python,list,import,text-files,Python,List,Import,Text Files,我有一个文本文件要作为列表导入,以便在此for while循环中使用: text_file = open("/Users/abc/test.txt", "r") list1 = text_file.readlines list2=[] for item in list1: number=0 while number < 5: list2.append(str(item)+str(number)) num

我有一个文本文件要作为列表导入,以便在此for while循环中使用:

text_file = open("/Users/abc/test.txt", "r")
list1 = text_file.readlines
list2=[]
    for item in list1:
        number=0
        while number < 5:
            list2.append(str(item)+str(number))
            number = number + 1
    print list2
text\u file=open(“/Users/abc/test.txt”,“r”)
list1=text_file.readlines
列表2=[]
对于列表1中的项目:
数字=0
当数量<5时:
列表2.追加(str(项目)+str(编号))
数字=数字+1
打印列表2
但当我运行这个时,它会输出:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'builtin_function_or_method' object is not iterable
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:“内置函数”或“方法”对象不可编辑
我该怎么办?

readlines()
是一种方法,称之为:

list1 = text_file.readlines()
另外,不要将整个文件加载到python列表中,而是逐行迭代文件对象。以及:


希望有帮助。

列表理解将帮助您:

print [y[1]+str(y[0]) for y in list(enumerate([x.strip() for x in open("/Users/abc/test.txt","r")]))]

它应该是
text\u file.readlines()
而不是
text\u file.readlines
with open("/Users/abc/test.txt", "r") as f:
    print [item.strip() + str(number) 
           for item in f 
           for number in xrange(5)]
print [y[1]+str(y[0]) for y in list(enumerate([x.strip() for x in open("/Users/abc/test.txt","r")]))]