在程序的第一部分,我打开了一个由用户提示的文件,然后编写代码依次读取文件的每一行,删除换行符,并将结果字符串附加到字符串列表中。
输入完成后,网格应显示在屏幕上。但是我的代码没有按照说明工作,需要帮助。下面是我到目前为止所做的代码:
x = {}
file = input("Enter a filename: ")
try:
a = open(file)
with open(file) as a:
x = [line.strip() for line in a]
a.close()
except IOError as e:
print ("File Does Not Exist")发布于 2015-08-18 05:16:41
不确定什么不起作用。你有一些不必要的代码。下面是一个适合我的版本:
# x = {}
file = input('enter a filename')
try:
# a = open(file)
with open(file) as a:
x = [line.strip() for line in a]
a.close()
except IOError as e:
print ("File Does Not Exist")
print(x)请注意,我已经注释掉了不必要的行,并添加了一条print语句,以便您可以看到x的内容
https://stackoverflow.com/questions/32059382
复制相似问题