How can I export data from a website and write the contents to a text file?

Michael Torrie torriem at gmail.com
Fri Nov 20 12:44:00 EST 2015


On 11/19/2015 12:17 PM, Patrick Hess wrote:
> 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.

It's not just a matter of line endings. It's a matter of text encoding
also.  This is critical in Python3 where everything is unicode and
encoding is essential.  You have to to use the text mode when writing
files here, and it's also a good idea to specify what encoding you wish
to write with (UTF-8 is a good default).




More information about the Python-list mailing list