Python nicht schreiben voller String in Datei

Ich versuche, eine ausgerichtete Proteinsequenz in eine Datei auszugeben, aber die Ausgabezeichenfolge wird jedes Mal, wenn ich versuche, die Zeichenfolge in die Datei zu schreiben, abgeschnitten. Der String, den ich versuche zu schreiben, ist 4603 Zeichen lang und wird mit der korrekten Anzahl von Zeichen auf die Konsole gedruckt. Das erste Code-Snippet, das ich anhabe, bezieht sich auf die Ausgangsvariable selbst. Ich werde die ganzen Saiten hier einschließen, denn die Art und Weise, in der die Saiten beim Schreiben in eine Akte abgeschnitten werden, ist für mich reproduzierbar.

out=open('out.7.9.12.txt','a+') score_dict={} Max='1ZER' al1='' score_dict[Max]=al1 for name,sequence in score_dict.items(): out.write('>') out.write(name) out.write('\n') for line in score_dict[name]: out.write(line) out.write('\n') 

Mit diesem Code-Snippet sollte die Ausgabe in der Datei gleich al1 sein. Allerdings ist die Ausgabe an die Datei wie folgt:

 XMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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SEXXXXXXKXXSXXXXXRYSXLXXXHXQNMXXXDKIXXXXXXXXXXKVXPTQXXIXXIWXXXXGXXXKQDXXXQVLXXDVXXXXSGADMLXXAKXXXSIAXNCQVXXEXXLXLEXXNCGXHSVVMEXRPRXXXKXTAKLXXIIDFLASVHNTDNNKKLDXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 

Einfügen dieser beiden Sequenzen in ein Textverarbeitungsprogramm und das Zählen der Zeichen zeigt, dass die erste 4603 Zeichen und die zweite hat 4090 Zeichen.

Ich entschuldige mich für die Sequenzblöcke hier, aber ich bin mir nicht sicher, wie sonst ich dieses Problem genau dargestellt haben könnte.

EDIT: Der Code sollte jetzt funktionieren (ich habe das Pufferproblem und das Zitatproblem behoben)

    4 Solutions collect form web for “Python nicht schreiben voller String in Datei”

    Schlief die Datei am Ende? Es kann sein, dass einige Ihrer Daten in den Puffer sitzen, also ist es gut zu schließen () die Datei, wenn Sie fertig sind (oder Flush () es, wenn Sie nicht wollen, um es aus irgendeinem Grund zu schließen).

    Besser noch, um die with konstrukt zu verwenden, die die Datei für Sie schließen wird, wenn Sie fertig sind oder eine Ausnahme auftritt:

     with open('out.7.9.12.txt','a+') as out: # the rest of your code indented under here # .... score_dict={} Max=245 

    Anmerkung aus den Flush () docs:

    Hinweis flush () schreibt nicht unbedingt die Daten der Datei auf Festplatte. Verwenden Sie flush () gefolgt von os.fsync () , um dieses Verhalten zu gewährleisten

    Verwenden Sie out.flush () am Ende (oder schließen ()), um Puffer zu löschen.

    Versuchen Sie, out.close() am Ende Ihres Skripts out.close() . Es wird sichergestellt, dass alle Ihre Ausgabe in die Datei gereinigt wird.

    Außerdem ist 'a +' redundant; 'A' ist Append-Modus, der bereits beschreibbar ist.

     Max = '245' al1 = 'XMXXXXXXXXXXXXXXXXXXX ...' scores = {Max:[al1]} with open('out.7.9.12.txt', 'a') as outf: for name,data in scores.iteritems(): outf.write('>{}\n{}\n'.format(name, '\n'.join(data))) 

    Ich habe diese genaue Frage. Ich habe festgestellt, dass ich es vernachlässigt habe, als Methode zu nennen. Ich habe foo.close anstelle von foo.close () benutzt.

    Das hat es gelöst

    Python ist die beste Programmiersprache der Welt.