Path: csiph.com!au2pb.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Michael Torrie Newsgroups: comp.lang.python Subject: Re: How can I export data from a website and write the contents to a text file? Date: Fri, 20 Nov 2015 10:44:00 -0700 Lines: 23 Message-ID: References: <9365cf2f-e9c7-4338-83b4-ce3d1d7ce1d6@googlegroups.com> <6e0f470b-f896-43ae-8f83-b20f22a9db8d@googlegroups.com> <2052054.MEiq1cOQgg@desk8.phess.net> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de bvsv4PAFmC4o9E2gOyxkJwjJgkraff1HQExNfI45HcEw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:text': 0.04; 'binary': 0.05; 'python3': 0.05; 'mode:': 0.07; 'skip:o 50': 0.07; 'subject:file': 0.07; 'subject:How': 0.09; '"w")': 0.09; 'essential.': 0.09; 'mode,': 0.09; 'encoding': 0.15; 'subject: \n ': 0.15; '"\\r\\n"': 0.16; 'default).': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:export': 0.16; 'wrote:': 0.16; 'working.': 0.18; 'together.': 0.20; 'this:': 0.23; 'header:In- Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'patrick': 0.27; 'specify': 0.27; 'correct': 0.28; 'idea': 0.28; 'actual': 0.28; 'subject:website': 0.29; 'system,': 0.30; 'open': 0.33; 'message-id:@gmail.com': 0.34; 'text': 0.35; 'unicode': 0.35; 'mode': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'operating': 0.37; 'received:org': 0.37; 'seem': 0.37; 'doing': 0.38; 'anything': 0.38; 'files': 0.38; 'subject:from': 0.39; 'subject:the': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'charset:windows-1252': 0.62; 'matter': 0.63; 'finally': 0.70; 'wish': 0.71; 'useful.': 0.72; 'subject:write': 0.84 X-Virus-Scanned: amavisd-new at torriefamily.org User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 In-Reply-To: <2052054.MEiq1cOQgg@desk8.phess.net> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99174 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).