为什么我的函数在python中返回空字符串?
我正在做的是,从文本中删除除名词以外的所有词类 我已经为此编写了一个函数。这可能不是最好或优化的代码,因为我刚刚开始用python编写代码。我确信这个错误一定是非常基本的,但我就是不能弄清楚它 在我的函数中,两个输入作为参数。一个是文本在硬盘上的位置,另一个是我们想要输出的文件的位置 下面是代码为什么我的函数在python中返回空字符串?,python,nltk,Python,Nltk,我正在做的是,从文本中删除除名词以外的所有词类 我已经为此编写了一个函数。这可能不是最好或优化的代码,因为我刚刚开始用python编写代码。我确信这个错误一定是非常基本的,但我就是不能弄清楚它 在我的函数中,两个输入作为参数。一个是文本在硬盘上的位置,另一个是我们想要输出的文件的位置 下面是代码 def extract_nouns(i_location, o_location): import nltk with open(i_location, "r") as myfile:
def extract_nouns(i_location, o_location):
import nltk
with open(i_location, "r") as myfile:
data = myfile.read().replace('\n', '')
tokens = nltk.word_tokenize(data)
tagged = nltk.pos_tag(tokens)
length = len(tagged)
a = list()
for i in range(0,length):
print(i)
log = (tagged[i][1][0] == 'N')
if log == False:
a.append(tagged[i][0])
fin = open(i_location, 'r')
fout = open(o_location, "w+")
for line in fin:
for word in a:
line = line.replace(word, "")
fout.write(line)
with open(o_location, "r") as myfile_new:
data_out = myfile_new.read().replace('\n', '')
return data_out
当我调用这个函数时,它工作得很好。我在硬盘上获得了预期的输出,但它没有返回接口上的输出,或者应该说,它返回的是一个空白字符串,而不是实际的输出字符串
这就是我所说的
t = extract_nouns("input.txt","output.txt")
如果您想尝试,请将以下内容作为输入文件的内容
"At eight o'clock on
Thursday film morning word line test
best beautiful Ram Aaron design"
这是我调用函数时在输出文件(output.txt)中得到的输出,但函数在接口上返回空字符串。它甚至不打印输出。
"
Thursday film morning word line test
Ram Aar design"
函数中的最后一条语句应该是
return
由于存在打印数据输出,因此返回的是打印的返回值,该值为无
例如:
您需要先关闭该文件:
for line in fin:
for word in a:
line = line.replace(word, "")
fout.write(line)
fout.close()
将与
一起使用通常是打开文件的最佳方式,因为它会自动关闭文件,并使用file.seek()
返回文件开头以读取:
def extract_nouns(i_location, o_location):
import nltk
with open(i_location, "r") as myfile:
data = myfile.read().replace('\n', '')
tokens = nltk.word_tokenize(data)
tagged = nltk.pos_tag(tokens)
length = len(tagged)
a = []
for i in range(0,length):
print(i)
log = (tagged[i][1][0] == 'N')
if not log:
a.append(tagged[i][0])
with open(i_location, 'r') as fin, open(o_location, "w+") as fout:
for line in fin:
for word in a:
line = line.replace(word, "")
fout.write(line)
fout.seek(0) # go back to start of file
data_out = fout.read().replace('\n' , '')
return data_out
您是否尝试过我在@alvas中建议的先前代码?这是一个完全不同的问题。这就是为什么我没有把它带过来。我已经试过你的建议了,一旦我弄明白了这个基本问题,我会的。我很好奇我的函数中有什么bug。你能帮我个忙吗?我不明白为什么人们对这件事投了反对票。虽然这些问题有相同的根据,但问题是完全不同的。如果我改变了内容,没有人会投反对票。仅仅因为代码相同并不意味着问题也相同。我没有投反对票,但我认为这是因为你的代码应该打开。StackOverflow用户在“我的代码怎么了?”问题上有点古怪您使用的是哪个版本的python?什么是错误追踪?天哪!我永远也想不到!非常感谢你,帕德雷克。我为这只虫子发疯了。谢谢!不用担心,我使用seek和with添加了代码来打开所有文件。如果将
与
一起使用,忘记关闭文件将不再是问题!
def extract_nouns(i_location, o_location):
import nltk
with open(i_location, "r") as myfile:
data = myfile.read().replace('\n', '')
tokens = nltk.word_tokenize(data)
tagged = nltk.pos_tag(tokens)
length = len(tagged)
a = []
for i in range(0,length):
print(i)
log = (tagged[i][1][0] == 'N')
if not log:
a.append(tagged[i][0])
with open(i_location, 'r') as fin, open(o_location, "w+") as fout:
for line in fin:
for word in a:
line = line.replace(word, "")
fout.write(line)
fout.seek(0) # go back to start of file
data_out = fout.read().replace('\n' , '')
return data_out