【问题标题】:convert list of strings from file to list of integers将字符串列表从文件转换为整数列表
【发布时间】:2016-04-26 23:11:57
【问题描述】:

我有一个用空格和逗号分隔的整数填充的大文件。我试图一次读取 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: ''

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

【问题讨论】:

  • 嗯,空字符串不是有效的以 10 为底的整数,这就是原因。你可以问if a:之前你做int_a = map(int, a)和/或检查/删除空字符串。
  • 也许您没有得到每 1000 个字节的格式正确的字符串?它适用于个位数的数字,但如果它开始变化,那么你会得到零碎的数字和逗号自己挂出来的部分。
  • 例如a = [int(x) for x in inf.read(1000).split(',') if x.strip().isdigit()] 如果您只使用整数
  • 为什么不做列表理解,或者创建一个自定义函数。 [int(i) if i else 0 for i in a] 或:def int_(i): return int(i) if i else 0map(int_, a)。这将假定空字符串应为 0。跳过它们,否则对空字符串进行列表推导。
  • 我不得不问 - 你想在这里用 TemporaryFile 做什么?

标签: python string list int


【解决方案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(', '))

【讨论】:

  • 感谢您的快速响应和详尽的解释。该文件太大以适合内存,我实际上正在学习/实施外部合并排序以处理这个特定问题。再次感谢!干杯。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-14
  • 2021-06-18
  • 1970-01-01
  • 2015-06-19
  • 2012-05-08
  • 1970-01-01
相关资源
最近更新 更多