对于作业我创建了一段代码。 目标是打开文件并从发件人中找到电子邮件地址。阅读与在Python中打开
#open file
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
#look for the appropriate lines and add the in a list
names = []
for line in fh:
line = line.rstrip()
if line.startswith("From:"):
words = line.split()
names.append(words[1])
我的问题是:为什么
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
#look for the appropriate lines and add the in a list
names = []
for line in fh:
line = line.rstrip()
if line.startswith("From:"):
words = line.split()
names.append(words[1])
工作,但
fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
text = read.fh()
#look for the appropriate lines and add the in a list
names = []
for line in text:
line = line.rstrip()
if line.startswith("From:"):
words = line.split()
names.append(words[1])
不会呢?
我很难理解为什么我只能使用open和not read。
感谢
'text = read.fh()'是错误的。使用'fh.read()' – adarsh 2015-03-30 19:25:25
它不工作,因为'read.fh'是完全荒谬的。它也行不通,因为你不再遍历行,而是遍历单个字符。最重要的是,“不起作用”对于SO来说不是一个可接受的错误描述。在与本网站进行任何进一步互动之前,请重新参观并浏览帮助中心。 – l4mpi 2015-03-30 19:32:58