gpt4 book ai didi

Python-将多行文本文件读取到列表中

转载 作者:行者123 更新时间:2023-11-30 22:04:57 24 4
gpt4 key购买 nike

我想将其转换(在 .txt 文件中):

7316717653
1330624919

进入这个(在Python列表中):

a_list = [7, 3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2, 4, 9, 1, 9]

我该怎么做?我到处寻找,但没有什么接近我想要做的事情

最佳答案

lst = []
with open('New Text Document.txt', 'r') as f: #open the file
lines = f.readlines() # combine lines of file in a list
for item in lines: # iterate through items of lines
# iterate through items in each line and remove extra space after last item in the line using strip method
for digit in item.strip():
lst.append(int(digit)) # append digits in to the list and convert them to int

print(lst)
[7, 3, 1, 6, 7, 1, 7, 6, 5, 3, 1, 3, 3, 0, 6, 2, 4, 9, 1, 9]

关于Python-将多行文本文件读取到列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53137975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com