gpt4 book ai didi

python - 在 stdout 上打印但无法在 Python 中写入文件

转载 作者:行者123 更新时间:2023-11-28 22:46:32 27 4
gpt4 key购买 nike

我突然对 Python 的这种奇怪行为感到非常沮丧。我一直在向文件写入各种数据,但从今天早上开始,它似乎就不起作用了。在发布之前我已经提到了所有这些:

已经尝试了以下所有命令,但它只是没有向 delete.txt 文件写入任何内容。发生了什么事?

fl=open('delete.txt','w')
fl.write(msg) <--Doesnt work,tried this also
fl.write('%s' %msg) <--Doesnt work,tried this also
fl.write("at least write this") <-- Doesnt work,tried this also
print (msg) <- WORKS

代码:

for i in hd_com.comment_message[1:500]:
fl=open('delete.txt','wb')

try:
if len(i)>40:
mes=processComUni(i)
proc=nltk.tokenize.word_tokenize(mes)
#print proc
pos=nltk.pos_tag(proc)
for i in pos:
if ((i[1]=="NN") or (i[1]=="NNP") or (i[1]=="NNS")) and len(i[0])>2:
#print i[0],i[1]
for j in home_depo_inv:
if i[0] in j.split() and (i[0]!='depot' and i[0]!='home' and i[0]!='store' and i[0]!='por' and i[0]!='get' and i[0]!='house' and i[0]!='find' and i[0]!='part' and i[0]!='son' and i[0]!='put' and i[0]!='lot' and i[0]!='christmas' and i[0]!='post'):
a=re.findall(i[0],j)
fl.write(str(i))<--Doesnt work,tried this also
fl.write(str(mes))<--Doesnt work,tried this also
fl.write("\n")<--Doesnt work,tried this also
fl.write("hello")<--Doesnt work,tried this also
fl.flush()
break
except:
continue
fl.close()

更多代码:

type(mes) = str
mes="omg would love front yard"

最佳答案

您的代码段的缩进完全搞砸了,但无论如何:您的代码以:

for i in hd_com.comment_message[1:500]:
fl=open('delete.txt','wb')

这意味着您在每次迭代时重新打开文件进行写入,删除上一次迭代可能写入的所有内容。

关于python - 在 stdout 上打印但无法在 Python 中写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27372620/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com