将字符串列表从文件转换为整数列表

时间:2016-04-26 23:11:58

标签: python string list int

我有一个大文件,里面填充了由空格和逗号分隔的整数。我试图一次读取1KB并将其转换为整数列表。

此代码可以正常工作:

with open('test_age.txt', 'r+') as inf:
    with open('test_age_out.txt', 'r+') as outf:
        sorted_list =[]
        a = [x.strip() for x in inf.read(1000).split(',')]
        int_a = map(int, a)
        f = tempfile.TemporaryFile()
        outf_array = sorted(int_a)
        f.write(str(outf_array))
        f.seek(0)
        #etc...

输出:

[1, 1, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, etc...

但是一旦我添加了一个while循环来读取下一个1KB:

with open('test_age.txt', 'r+') as inf:
    with open('test_age_out.txt', 'r+') as outf:
        sorted_list =[]
        while True:
            a = [x.strip() for x in inf.read(1000).split(',')]
            int_a = map(int, a)
            if not a:
                break
            f = tempfile.TemporaryFile()
            outf_array = sorted(int_a)
            print outf_array
            f.write(str(outf_array))
            f.seek(0)      

我得到输出和ValueError:

[1, 1, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 
8, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 12, 12, 12,
12, 12, 12, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 15, 15, 16, 17, 18,
19, 19, 20, 20, 20, 20, 21, 21, 22, 22, 22, 23, 23, 24, 24, 24, 24, 25, 
25, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 28, 28, 29, 30, 30, 30, 30,
31, 31, 31, 32, 32, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35,
35, 35, 35, 36, 36, 37, 37, 37, 37, 38, 38, 39, 39, 39, 39, 39, 39, 40,
40, 40, 40, 41, 41, 42, 43, 43, 43, 44, 44, 44, 44, 44, 45, 46, 46, 46,
46, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 48, 49, 49, 49, 50, 50, 50,
50, 50, 50, 51, 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 52, 53, 53, 54,
54, 54, 55, 55, 55, 55, 56, 56, 56, 56, 56, 57, 57, 57, 57, 58, 58, 58,
59, 59, 60, 60, 60, 61, 62, 62, 62, 62, 63, 63, 63, 63, 63, 63, 63, 64,
64, 64, 65, 66, 66, 67, 67, 67, 67, 68, 68, 68, 68, 68, 69, 69, 69, 69, 
69, 69, 69, 70, 70, 70, 70, 71, 71, 72, 72, 73, 74, 74, 74, 75, 76, 76,
76, 76, 77, 77, 77, 77, 78, 78, 79, 79, 79, 79, 81, 81, 81, 81, 82, 82, 
82, 82, 82, 83, 83, 83, 83, 84, 85, 85, 85, 85, 86, 86, 86, 87, 87, 87,
87, 87, 87, 88, 88, 88, 88, 88, 88, 88, 89, 89, 89, 89, 90, 90, 90, 91,
91, 91, 91, 91, 91, 91, 92, 92, 93, 93, 93, 94, 94, 94, 94, 95,  95,
96, 96, 96, 97, 97, 98, 99, 100, 100, 100, 100, 100]
[2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 8, 9, 10, 10, 11, 11, 11, 11, 12, 12,12, 
13, 14, 15, 17, 17, 17, 17, 17, 17, 18, 18, 18, 20, 21, 22, 22, 22, 22, 
23, 23, 24, 24, 24, 26, 27, 27, 27, 27, 28, 28, 29, 29, 29, 29, 30, 32, 
32, 32, 32, 33, 33, 34, 34, 36, 37, 37, 37, 37, 38, 39, 41, 41, 42, 43,   
44, 44, 46, 46, 47, 48, 49, 49, 49, 49, 51, 51, 52, 52, 52, 52, 53, 54, 
54, 54, 55, 55, 56, 60, 60, 61, 61, 61, 62, 63, 63, 64, 65, 65, 65, 65, 
66, 66, 67, 68, 68, 68, 70, 70, 73, 73, 73, 74, 74, 75, 75, 75, 77, 77, 
77, 77, 78, 78, 78, 78, 79, 80, 81, 81, 82, 82, 83, 83, 83, 83, 84, 84, 
85, 85, 85, 85, 86, 87, 88, 90, 91, 91, 91, 92, 93, 93, 93, 94, 95, 97, 
98, 98, 99, 100]
    int_a = map(int, a)
ValueError: invalid literal for int() with base 10: ''

我不确定为什么会这样。如果我调用print,似乎创建和排序列表。但是ValueError存在。是什么给了什么?

1 个答案:

答案 0 :(得分:1)

查看str.split的输出,其中传递的分隔符出现在字符串的头部或尾部:

>>> ', 3, 5'.split(', ')
['', '3', '5']

该空字符串是您的程序正在尝试(并且失败)解析为整数的字符串。 ''.strip()没有帮助(顺便说一下,int()并不是必需的 - 它会自动忽略前导和尾随空格)。我建议读取保证完整且有效的块,例如行。如果文件只是一个大行,你将不得不做一些额外的工作来保存一行中的最后一个字符并将它们移动到下一行的处理中。不要忘记在循环后处理剩余的字符。

line = inf.read(1000)
new += line
current, delimiter, new = line.rpartition(', ')
# process current
# continue loop to add more content

如果文件可以舒适地放入系统的内存中,您可以只读取整个文件并将其拆分:

numbers = map(int, inf.read().split(', '))