如何在阅读行中分割行并将其保存在其他列表中?

这是我的代码

with open('file.txt','r') as source:
    # Indentation
    polTerm = [line.strip().split()[0] for line in source.readlines()]
    polFreq = [int(line.strip().split()[1]) for line in source.readlines()]

这是在file.txt中

anak 1
aset 3
atas 1
bangun 1
bank 9
benar 1
bentuk 1

我得到了想要的polTerm:

  

['anak','aset','atas','bangun','bank','benar','bentuk']

,但使用polFreq代替:

  

['1','3','1','1','9','1','1']

我得到的是这样的空白列表:

  

[]

有人知道为什么会这样吗?以及如何解决这个问题,这样我就可以得到我想要的东西。

yyuuzzjj 回答:如何在阅读行中分割行并将其保存在其他列表中?

with open('file.txt','r') as source:
    lines = source.readlines()
    polTerm = [line.strip().split()[0] for line in lines]
    polFreq = [int(line.strip().split()[1]) for line in lines]

原因是readlines()是一个迭代器,因此第一个调用已经消耗了它,并且它变成空的,当您第二次尝试使用该空的迭代器时,您发现它是空的。

,

正如Carcigenicate所说,.readlines是一个返回列表的生成器。如果您不将该列表保存在变量中,则第二次调用生成器将不返回任何内容,因为生成器在您的第一次调用中已用尽。您想要的是什么

with open("file.txt","r") as inf:
    # Now your lines list is saved in a global variable 
    # which can be used outside with open().
    # The .readlines generator is exhausted and won't return 
    # anything.
    raw = inf.readlines()

polTerm = [line.strip().split()[0] for line in raw]
polFreq = [int(line.strip().split()[1]) for line in raw]

专业提示:学习使用熊猫,特别是pd.read_csv()。

,
with open('file.txt','r') as source:
     data=source.readlines()
a1=[] 
a2=[] 
for line in data:
     x=line.split()
     a1.append(x[0])
     a2.append(x[1])
,

@Carcgenicate给您字面上的答案。

但是,在我看来,您不应该读取文件两次(除非文件确实很大,并且所有行都无法放入内存中。

如果文件不是那么大,则无需两次读取文件。 如果它有点大,那么只需将前两列读入内存即可。 然后将它们分开。

我建议的是:

with open('file.txt','r') as source:
    cols_1_and_2 = [line.strip().split(None,2)[:2] for line in source.readlines()]

polTerm = [cols[0] for cols in cols_1_and_2]
polFreq = [int(cols[1]) for cols in cols_1_and_2]
del cols_1_and_2  # this line is to free some memory if that would be an issue
本文链接:https://www.f2er.com/3169576.html

大家都在问