【问题标题】:writing a simple string to a .txt file in python suddenly doesn't work在 python 中将一个简单的字符串写入 .txt 文件突然不起作用
【发布时间】:2020-03-21 22:49:49
【问题描述】:

我不知道为什么这些代码行会突然停止工作。 我刚刚在 jupyter notebook 上重新启动了我的内核,但无论出于何种原因,它都没有将字符串写入我指定的文本文件。我的代码是这样的:

Code = 'H His\nQ Gln\nP Pro\nR Arg\nL Leu\nD Asp\nE Glu\nA Ala\nG Gly\nV Val\nY Tyr\n  STP\nS Ser\nC Cys\nW Trp\nF Phe\nN Asn\nK Lys\nT Thr\nS Ser\nI Ile\nM Met'
f=open('AA_Codon_code.txt','w+')
f.write(Code)
f.close

文本文件突然没有任何内容,即使它显示在我的目录中。文本文件中没有文本。

当我尝试读取数据时果然是空的。

data = open('AA_Codon_code.txt','r')
amino_acids = data.read()
peptide_dict = {}
amino_acids

这个方法我用过很多次了,但是现在不行了。为什么会这样?

我只想要一个 .txt 文件,上面写着这个字符串:

'H His\nQ Gln\nP Pro\nR Arg\nL Leu\nD Asp\nE Glu\nA Ala\nG Gly\nV Val\nY Tyr\n STP\nS Ser\nC Cys\nW Trp\nF Phe\nN Asn\nK Lys\nT Thr\nS Ser\nI Ile\nM Met'

【问题讨论】:

    标签: python file text


    【解决方案1】:

    f.close 只是一个方法的名称。要调用此方法,您需要括号:

    f.close()
    

    【讨论】:

    • 好尴尬。非常感谢
    猜你喜欢
    • 2016-10-27
    • 1970-01-01
    • 2014-06-02
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 2020-09-12
    • 1970-01-01
    相关资源
    最近更新 更多