输入(在文件中)强文本:
0::564::1688
1::778::1650
...
输出:
[[0, 564, 1688], [1, 778, 1650], ...]
假设每一行都是一个字符串:
>>> s = """0::564::1688
1::778::1650"""
>>> [i.split("::") for i in s.split("\n")]
[['0', '564', '1688 '], ['1', '778', '1650']]
如果您想在执行过程中将项目从 string 转换为 int:
>>> [[int(x) for x in i.split("::")] for i in s.split("\n")]
[[0, 564, 1688], [1, 778, 1650]]
text = """
0::564::1688
1::778::1650
"""
print(text.split('\n'))
lines = text.split('\n')
totalList = []
for line in lines:
if line.__len__ == 0: continue
words = line.split('::')
numberList = []
for word in words:
if(word.__len__ == 0): continue
try:
print(int(word))
numberList.append(int(word))
except:
print()
totalList.append(numberList)