【发布时间】:2015-05-20 02:39:32
【问题描述】:
我想使用 Python 从 url 中读取数字列表,例如:
1,2,3,4,5,6
2,3,2,3,2,3
etc
我试过了:
list.append([int(n) for n in line.strip().split(',')])
但我使用的是 Python 3.2,这会给出错误:“TypeError: Type str doesn't support the buffer API”,因为 Python 3 将输入读取为字节。
所以我尝试将行转换为字符串:
list.append([int(n) for n in str(line).strip().split(',')])
但现在我在每行的开头都有 b' 错误:ValueError: invalid literal for int() with base 10: "b'1"
有没有一种优雅的方式来获取这些数字,还是我需要捕获字符串,去掉前两个字符,然后转换为 int?
【问题讨论】:
标签: python python-3.x