Python Forum
How to work with large lists without crashing Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to work with large lists without crashing Python?
#9
Put the data into a file. Let's call it data.txt.

with open('data.txt', 'r') as data:
    for line in data:
        # do something
Here is how you read from a file. You create a file object called data and iterate over each line. The 'r' parameter is for reading.
To write to a file you do almost the same.
with open('some_file.txt', 'w') as output: # this time in 'w' (write) mode
    for element in iterable:
        output.write(element)
The with statement is called context manager and it will close the file automatically when its code block is executed. You can stack them.

with open('file_one.txt', 'r') as r:
    with open('file_two.txt', 'w') as w:
        for line in r:
            w.write(line)
This read from one file and write to another. Basically, it's copying the file.
tester_V likes this post
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
RE: How to work with large lists without crashing Python? - by wavic - Mar-14-2018, 09:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python/Numpy have I already written the swiftest code for large array? justforkicks1 1 2,825 Dec-12-2017, 11:56 PM
Last Post: squenson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020