首先,我的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()
答案 0 :(得分:2)
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()
答案 1 :(得分:0)
编辑:错误的语言。
with open("file.txt") as f:
ints = [int(line) in f.readlines()]