Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Patrick Hess Newsgroups: comp.lang.python Subject: Re: How can I export data from a website and write the contents to a text file? Date: Thu, 19 Nov 2015 20:17:37 +0100 Lines: 26 Message-ID: References: <9365cf2f-e9c7-4338-83b4-ce3d1d7ce1d6@googlegroups.com> <6e0f470b-f896-43ae-8f83-b20f22a9db8d@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de LoEdQfVNWDSKZuT0vqniTQGI6BpU4dFpqRbgMhROtNaw== 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; 'mode:': 0.07; 'skip:o 50': 0.07; 'subject:file': 0.07; 'subject:How': 0.09; '"w")': 0.09; 'mode,': 0.09; 'received:gmx.net': 0.09; '"\\r\\n"': 0.16; '2")': 0.16; 'from:addr:gmx.net': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:export': 0.16; 'wrote:': 0.16; 'typing': 0.18; 'working.': 0.18; ';-)': 0.18; 'together.': 0.20; 'produces': 0.22; 'this:': 0.23; 'unix': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'patrick': 0.27; 'correct': 0.28; 'actual': 0.28; 'subject:website': 0.29; 'system,': 0.30; 'windows.': 0.33; 'open': 0.33; 'text': 0.35; 'mode': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'operating': 0.37; 'charset:us-ascii': 0.37; 'seem': 0.37; 'doing': 0.38; 'anything': 0.38; 'files': 0.38; 'subject:from': 0.39; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'received:212.227.17': 0.66; 'finally': 0.70; 'useful.': 0.72; 'subject:write': 0.84 User-Agent: KMail/4.14.3 (FreeB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Uv v9Od5TctZltUI43GFU1aIw/8I6RjgbocEopFNKZ2pRby6NsQCS2PyRMZPUJ4rgnZBsEpymTKI pBmYzWP X-Mailman-Approved-At: Thu, 19 Nov 2015 15:23:37 -0500 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:99093 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