我正在尝试从文件读取并将其附加到python中的数组。 但是当我使用时:
while True:
repos.append(fileRepos.readline())
print(repos[counter])
counter+=1
if counter > 5274:
break
问题是我的文件“ fileRepos”有5274行。每行应包含一个存储库的名称。但是当我打印出repos
数组时,有些对象是空的。我的猜测是它读取一些空格和某些行作为对象。但是它不应该能够像txt文件一样读取整行吗?我该怎么解决?
文件如下:
repos.txt:
aco-palinda-1
aco-palinda-2
aco-palinda-3
aco-quicksort
aco-week-1
acol-week-10
aco-week-11
aco-week-12
aco-week-13
答案 0 :(得分:1)
使用:
repos = []
with open(filename) as infile: #Open file for read
for line in infile: #Iterate Each line
line = line.strip() #Strip leading and trailing space
if line: #Check if line is empty
repos.append(line) #Append repo name to list.