[英]How do I convert a list in a .txt file to a list in Processing (python)?
[英]How to convert txt file into list of integers
首先,我的txt文件數據如下:
51
2
2
49
15
2
1
14
我想將它們轉換為列表以進行進一步的計算,但是我做不到,我嘗試使用for循環逐一打印。 然后錯誤消息“屬性錯誤”不斷出現。
def main():
file = str(input("Please enter the full name of the desired file(with extension) at the prompt below: \n"))
print (get_value(file))
def get_value(file):
file_open = open(file,"r")
print (file_open.read())
a = len(file)
print ("Length =",a)
for line in range file_open:
print (line)
main()
def main():
file = str(input("Please enter the full name of the desired file(with extension) at the prompt below: \n"))
print (get_value(file))
def get_value(file):
file_open = open(file,"r")
lsLines = file_open.readlines()
lsLines = [int(x) for x in lsLines if len(x.strip()) > 0]
file_open.close()
return lsLines
main()
編輯:錯誤的語言。
with open("file.txt") as f:
ints = [int(line) in f.readlines()]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.