How can I export data from a website and write the contents to a text file?
Patrick Hess
patrickhess at gmx.net
Thu Nov 19 14:17:37 EST 2015
ryguy7272 wrote:
> text_file = open("C:/Users/rshuell001/Desktop/excel/Text1.txt", "wb")
> [...]
> It doesn't seem like the '\n' is doing anything useful. All the text is jumbled together.
> [...]
> I finally got it working. It's like this:
> "\r\n"
The better solution would be to open text files in actual text mode:
open("filename", "wb") # binary mode
open("filename", "w") # text mode
In text mode, the correct line-ending characters, which will vary
depending on the operating system, are chosen automatically.
with open("test.txt", "w") as textfile:
textfile.write("line 1\n")
textfile.write("line 2")
This produces "line 1\nline 2" on Unix systems and "line 1\r\nline 2"
on Windows.
Also involves less typing this way. ;-)
Patrick
More information about the Python-list
mailing list