Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99093

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

From Patrick Hess <patrickhess@gmx.net>
Newsgroups comp.lang.python
Subject Re: How can I export data from a website and write the contents to a text file?
Date 2015-11-19 20:17 +0100
Message-ID <mailman.486.1447964619.16136.python-list@python.org> (permalink)
References <9365cf2f-e9c7-4338-83b4-ce3d1d7ce1d6@googlegroups.com> <6e0f470b-f896-43ae-8f83-b20f22a9db8d@googlegroups.com> <e0edf996-9ce8-404e-b4e0-1e9a7b9af706@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How can I export data from a website and write the contents to a text file? ryguy7272 <ryanshuell@gmail.com> - 2015-11-18 08:37 -0800
  Re: How can I export data from a website and write the contents to a text file? Chris Angelico <rosuav@gmail.com> - 2015-11-19 03:57 +1100
    Re: How can I export data from a website and write the contents to a text file? ryguy7272 <ryanshuell@gmail.com> - 2015-11-18 09:03 -0800
      Re: How can I export data from a website and write the contents to a text file? ryguy7272 <ryanshuell@gmail.com> - 2015-11-18 09:15 -0800
  Re: How can I export data from a website and write the contents to a text file? Denis McMahon <denismfmcmahon@gmail.com> - 2015-11-18 17:19 +0000
    Re: How can I export data from a website and write the contents to a text file? ryguy7272 <ryanshuell@gmail.com> - 2015-11-18 09:40 -0800
      Re: How can I export data from a website and write the contents to a text file? ryguy7272 <ryanshuell@gmail.com> - 2015-11-18 09:43 -0800
        Re: How can I export data from a website and write the contents to a text file? Patrick Hess <patrickhess@gmx.net> - 2015-11-19 20:17 +0100
        Re: How can I export data from a website and write the contents to a text file? Michael Torrie <torriem@gmail.com> - 2015-11-20 10:44 -0700
      Re: How can I export data from a website and write the contents to a text file? Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-11-18 18:05 +0000
  Re: How can I export data from a website and write the contents to a text file? Random832 <random832@fastmail.com> - 2015-11-18 16:38 -0500

csiph-web