我想读作int
,是否有任何pythonic方法可以做到这一点?
f = open('p059_cipher.txt', 'rU')
holder = list((f.read().replace('"', '').split(',')))
Letters = list()
for number in holder:
Letters.append(int(number))
答案 0 :(得分:1)
我试图从您的输入中猜测,但这可能会有效:
from ast import literal_eval
with open('p059_cipher.txt') as f:
data = "[" + f.read() + "]"
result = list(map(int, literal_eval(data)))
# The call to list is only necessary if both
# 1. You explicitly need a LIST
# 2. You're running Python3
# If you're in Python2 or you just need to iterate, ignore the list call
这应该采取如下输入:
"1", "2", "3", "4", "12", "1003981890213"
并创建
result == [1, 2, 3, 4, 12, 1003981890213]
答案 1 :(得分:1)
尝试以下方法:
with open('p059_cipher.txt', 'rU') as f:
numbers = list(map(int, f.read().replace('"','').split(',')))
这会将包含1,2,"3"
的文件转换为[1, 2, 3]
(并将其另存为numbers
)。