我有一个具有5000行的文本文件。格式是这样的:
1,3,4,1,2,3,5,build
2,6,4,6,7,3,4,demolish
3,6,10,2,3,1,3,demolish
4,4,1,2,3,4,5,demolish
5,1,1,1,1,6,8,build
例如,我要列出其他列表: 第二列:
second_build=[3,1]
second_demolish=[6,6,4]
我已经尝试过类似的事情:
with open('cons.data') as file:
second_build=[line.split(',')[1] for line in file if line.split(',')[7]=='build']
但这没用。
答案 0 :(得分:3)
您可以按以下方式获取每个列/操作的值:
lines = """1,3,4,1,2,3,5,build
2,6,4,6,7,3,4,demolish
3,6,10,2,3,1,3,demolish
4,4,1,2,3,4,5,demolish
5,1,1,1,1,6,8,build""".split(
"\n"
)
build_cols = [list() for _ in range(7)]
demolish_cols = [list() for _ in range(7)]
data = {"build": build_cols, "demolish": demolish_cols}
for line in lines:
tokens = line.split(",")
for bc, tok in zip(data[tokens[-1]], tokens):
bc.append(tok)
# to access second column build values:
print(build_cols[1])
# ['3', '1']
例如,build_cols
存储一个列表列表,每个条目代表一列。对于每个构建行,您都将适当列中的项目附加到build_cols
中的相应位置。
答案 1 :(得分:2)
只需简单地首先将readlines
设置为变量,然后在列表理解中简单地添加一个rstrip
就可以了,因为值(最后一个除外)都在'\n'
最后,将它们剥离,并使其成为整数:
with open('cons.data') as file:
f=file.readlines()
second_build=[int(line.split(',')[1]) for line in f if line.rstrip().split(',')[-1]=='build']
second_demolish=[int(line.split(',')[1]) for line in f if line.rstrip().split(',')[-1]=='demolish']
现在:
print(second_build)
print(second_demolish)
是:
[3, 1]
[6, 6, 4]