Read a text file into a list.

This script stores the lines of a text file into a list. But a file could be big, so store them in a list only if necessary. If you can get away with processing one line at a time, discarding each line before you go on to the next line, then you don’t need to store the lines into a list. Our first example was here.

But the following script sorts the lines into alphabetical order, so it has to hold all the lines simultaneously in a list. Before runing the script, save the file bond.txt onto your Desktop. In the script, change myname or Myname to your name.

bond.py

1962 Dr. No                          1985 A View to a Kill
1963 From Russia with Love           1967 Casino Royale
1964 Goldfinger                      2006 Casino Royale
1965 Thunderball                     1971 Diamonds Are Forever
1967 Casino Royale                   2002 Die Another Day
1967 You Only Live Twice             1962 Dr. No
1969 On Her Majesty's Secret Service 1981 For Your Eyes Only
1971 Diamonds Are Forever            1963 From Russia with Love
1973 Live and Let Die                1995 GoldenEye
1974 The Man with the Golden Gun     1964 Goldfinger
1977 The Spy Who Loved Me            1989 Licence to Kill
1979 Moonraker                       1973 Live and Let Die
1981 For Your Eyes Only              1979 Moonraker
1983 Octopussy                       1983 Never Say Never Again
1983 Never Say Never Again           1983 Octopussy
1985 A View to a Kill                1969 On Her Majesty's Secret Service
1987 The Living Daylights            2008 Quantum of Solace
1989 Licence to Kill                 2012 Skyfall
1995 GoldenEye                       2015 Spectre
1997 Tomorrow Never Dies             1987 The Living Daylights
1999 The World Is Not Enough         1974 The Man with the Golden Gun
2002 Die Another Day                 1977 The Spy Who Loved Me
2006 Casino Royale                   1999 The World Is Not Enough
2008 Quantum of Solace               1965 Thunderball
2012 Skyfall                         1997 Tomorrow Never Dies
2015 Spectre                         1967 You Only Live Twice

Things to try

  1. Instead of splitting the file into lines as we input it with readlines, we can slurp the entire file into one big string. Later, we can split the string into separate lines. Change line 27 of bond.py from
    lines = infile.readlines()   #lines is a list of strings
    
    to
    oneBigString = infile.read() #Slurp the entire file.  oneBigString is one big string.
    
    Then after you close the file, but before you sort,
    lines = oneBigString.splitlines() #lines is a list of strings
    
    Remove the two calls to rstrip in line 32, because splitlines has already removed the newlines.
  2. Let’s do the same thing, but this time we’ll download the file from the web.
    import sys
    import urllib.request
    
    url = "http://oit2.scps.nyu.edu/~meretzkm/python/list/bond.txt"
    
    try:
        infile = urllib.request.urlopen(url)
    except urllib.error.URLError as error:
        print(error, file = sys.stderr)
        sys.exit(1)
    
    bigSequenceOfBytes = infile.read()
    infile.close()
    
    try:
        oneBigString = bigSequenceOfBytes.decode("utf-8")
    except UnicodeError as error:
        print(error, file = sys.stderr)
        sys.exit(1)
    
    lines = oneBigString.splitlines()   #lines is a list of strings.
    sortedLines = sorted(lines, key = lambda line: line[5:])   #alphabetical order by movie name
    
    for line, sortedLine in zip(lines, sortedLines):
        print(f"{line:36} {sortedLine}")
    
    1962 Dr. No                          1985 A View to a Kill
    1963 From Russia with Love           1967 Casino Royale
    1964 Goldfinger                      2006 Casino Royale
    1965 Thunderball                     1971 Diamonds Are Forever
    1967 Casino Royale                   2002 Die Another Day
    1967 You Only Live Twice             1962 Dr. No
    1969 On Her Majesty's Secret Service 1981 For Your Eyes Only
    1971 Diamonds Are Forever            1963 From Russia with Love
    1973 Live and Let Die                1995 GoldenEye
    1974 The Man with the Golden Gun     1964 Goldfinger
    1977 The Spy Who Loved Me            1989 Licence to Kill
    1979 Moonraker                       1973 Live and Let Die
    1981 For Your Eyes Only              1979 Moonraker
    1983 Octopussy                       1983 Never Say Never Again
    1983 Never Say Never Again           1983 Octopussy
    1985 A View to a Kill                1969 On Her Majesty's Secret Service
    1987 The Living Daylights            2008 Quantum of Solace
    1989 Licence to Kill                 2012 Skyfall
    1995 GoldenEye                       2015 Spectre
    1997 Tomorrow Never Dies             1987 The Living Daylights
    1999 The World Is Not Enough         1974 The Man with the Golden Gun
    2002 Die Another Day                 1977 The Spy Who Loved Me
    2006 Casino Royale                   1999 The World Is Not Enough
    2008 Quantum of Solace               1965 Thunderball
    2012 Skyfall                         1997 Tomorrow Never Dies
    2015 Spectre                         1967 You Only Live Twice