如何对文本文件中的数字进行分类?

时间:2018-12-04 02:12:37

标签: python python-3.x

我有一个具有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']

但这没用。

2 个答案:

答案 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]