- • "\n" unter Unix
• "\r\n" unter Windows
• "\r" unter Mac OS 9 etc.
Hab davon auch schonmal was gehört, aber weder in meinem Büchlein oder dem Forum hier was konkretes gelesen. Gibt's sowas?
Hi droptix!droptix hat geschrieben:wäre eine Konstante wie `newline` sinnvoll.
Code: Alles auswählen
f = file(r"filename.txt", "w")
f.write("asdf\n")
f.close()
Was hat das mit meinem Problem zu tun?BlackJack hat geschrieben:Genau das ist doch der Sinn der Unterscheidung von Text- und Binärdateien.
Aaaaaah! Da haste wohl recht! Bei mir tritt das Problem auf, weil ichgerold hat geschrieben:Gibt es --> ``os.linesep``
Wenn du aber eine Datei so ``f = file("filename.txt", "w)`` öffnest, dann übernimmt Python das Schreiben der korrekten Zeilenenden.
Dieser Code […] Schreibt unter Linux ein "\n", unter Mac ein "\r" und unter Windows ein "\r\n" in die Datei.
Code: Alles auswählen
x = ["spam", "eggs"]
f = file(r"filename.txt", "w")
f.writelines(x)
f.close()
Code: Alles auswählen
spameggs
Code: Alles auswählen
x = ["spam", "eggs"]
f = file(r"filename.txt", "w")
f.write(os.linesep.join(x))
f.close()
Python Documentation hat geschrieben:writelines( sequence)
Write a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value. (The name is intended to match readlines(); writelines() does not add line separators.)
Hi droptix!droptix hat geschrieben:Code: Alles auswählen
x = ["spam", "eggs"] f = file(r"filename.txt", "w") f.write(os.linesep.join(x)) f.close()
Code: Alles auswählen
#so
x = ["spam", "eggs"]
f = file(r"filename.txt", "w")
f.write("\n".join(x))
f.close()
# oder so
x = ["spam", "eggs"]
f = file(r"filename.txt", "w")
f.writelines("%s\n" % line for line in x)
f.close()
# oder so
x = ["spam", "eggs"]
f = file(r"filename.txt", "w")
for line in x:
f.write(line + "\n")
f.close()
Code: Alles auswählen
x = ["spam", "eggs"]
f = file(r"filename.txt", "w")
f.write(os.linesep.join(x))
f.close()
Code: Alles auswählen
x = ["spam", "eggs"]
f = file(r"filename.txt", "wb")
f.write(os.linesep.join(x))
f.close()
Code: Alles auswählen
>>> f = file(r"eintext.txt", "w")
>>> f.write("asdf" + os.linesep)
>>> f.write("jjjj" + os.linesep)
>>> f.close()
>>> f = file(r"eintext.txt", "rb")
>>> print repr(f.read())
'asdf\r\r\njjjj\r\r\n'
>>>