简体   繁体   English

读取txt文件并放入python列表

[英]Read txt file and put in list with python

I have a file with text with only 0's and 1's. 我有一个只有0和1的文本文件。 No space between them. 他们之间没有空间。 Example: 例:

0101110 0101110

1110111 1110111

I would like to read a character at a time and place them as a single element in a list of integers. 我想一次读取一个字符,并将它们作为单个元素放置在整数列表中。

My code: 我的代码:

intlist = []

with open('arq.txt', 'r') as handle:
for line in handle:
    if not line.strip():
        continue

    values = map(int, line.split())
    intlist.append(values)

print intlist
handle.close()

My result: 我的结果:

[[101110], [1110111]] [[101110],[1110111]]

Like if I transform the 0101110 in intlist = [0,1,0,1,1,1,0,1,1,1,0,1,1,1]. 就像我在intlist = [0,1,0,1,1,1,0,1,1,1,0,1,1,1]中转换0101110一样。 (without '\\n') (不带“ \\ n”)

You just need two changes. 您只需要进行两项更改。 The first is, when you're making values , you need to take each individual character of the line, instead of the entire line at once: values = [int(x) for x in line.strip() if x] 第一个是,当您创建values ,需要获取行的每个字符,而不是一次获取整个行: values = [int(x) for x in line.strip() if x]

Now, if you run this code on its own, you'll get [[1, 0, ...], [1, 1, ...]] . 现在,如果您单独运行此代码,则会得到[[1, 0, ...], [1, 1, ...]] The issue is that if you call list.append with another list as an argument, you'll just add the list as an element. 问题是,如果您调用list.appendlist.append另一个列表作为参数,则只会将该列表添加为元素。 You're looking for the list.extend method instead: 您正在寻找list.extend方法:

intlist.extend(values)

Your final code would be: 您的最终代码为:

intlist = []

with open('arq.txt', 'r') as handle:
    for line in handle:
        if not line.strip():
            continue

        values = [int(x) for x in line.strip() if x]
        intlist.extend(values)

    print intlist

Here's one way you can do it. 这是您可以做到的一种方法。 You also don't need to use handle.close() the context manager handles closing the file for you. 您也不需要使用handle.close() ,上下文管理器会为您关闭文件。

intlist = []

with open('arq.txt', 'r') as handle:
    for line in handle:
        if not line.strip():
            continue
        intlist[:] += [int(char) for i in line.split() for char in i]

print(intlist)

Use strip() for delete \\n and filter with bool for deleting empty strings: 使用strip()删除\\ n并使用bool 过滤以删除空字符串:

with open('test.txt') as f:
    lines = map(lambda x: x.strip(), filter(bool, f))
    print [int(value) for number in lines for value in number]

One possibility: 一种可能性:

intlist = []

with open('arq.txt', 'r') as handle:
    for line in handle:
        for ch in line.strip():
            intlist.append (ch)

print intlist

If all you have is numbers and linefeeds, you can just read the file, remove the linefeeds, map the resulting string to integers, and turn that into a list : 如果只有数字和换行符,则只需阅读文件,删除换行符,将结果字符串映射为整数,然后将其转换为list

with open('arq.txt') as handle:
    intlist = list(map(int, handle.read().replace('\n', '')))

print intlist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM