See Lists may be constructed in several ways.
[
square
brackets]
.
months = [ "January", #0 "February", #1 "March" #2 ]
myList = 5 * [0] for i in myList: print(i)
0 0 0 0 0
append
or
insert
.
See
CSV.
myList = [] #Start with an empty list. for var in something: myList.append(anAdditionalItem)
bigList = [10, 20, 30] + [40, 50, 60] bigList += [70, 80, 90] print(bigList)
[10, 20, 30, 40, 50, 60, 70, 80, 90]
splitlines
.
preamble = """\ We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty, and the pursuit of Happiness. """ phrases = preamble.split(",\n") #phrases is a list of 4 strings.
list
.
See
Split.
fdr = "Franklin Delano Roosevelt" characters = list(fdr) #characters is a list of 25 strings, each of length 1
myString
is a
string
containing comma separated values, and
[myString]
is a
list
containing one string.
Since
[myString]
contains only one item, we call
next
only one time.
myString
could come from a text file;
see
CSV.
import csv
reader = csv.reader([myString]) #[myString] is a list containing one string fields = next(reader) #fields is a list of strings.
from disk | from web | |
---|---|---|
Read the file
into one big string of characters or one big sequence of bytes; then split it into lines using splitlines. |
import sys filename = "/Users/etc." try: infile = open(filename) except FileNotFoundError as error: print(error, file = sys.stderr) sys.exit(1) except PermissionError as error: print(error, file = sys.stderr) sys.exit(1) bigString = infile.read() #bigString is one big string. infile.close() lines = bigString.splitlines() #lines is a list of strings for line in lines: #line is a string print(line) #line does not end with a newline |
import sys import urllib.request url = "http://etc." 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: #Convert sequence of bytes to string of characters. bigStringOfCharacters = bigSequenceOfBytes.decode("utf-8") except UnicodeError as error: print(error, file = sys.stderr) sys.exit(1) #lines is a list of strings lines = bigStringOfCharacters.splitlines() for line in lines: print(line) #line does not end with a newline |
Read the file
into a list of lines using readlines. |
import sys filename = "/Users/etc." try: infile = open(filename) except FileNotFoundError as error: print(error, fie = sys.stderr) sys.exit(1) except PermissionError as error: print(error, file = sys.stderr) sys.exit(1) lines = infile.readlines() #lines is a list of strings infile.close() for line in lines: #end="" because each line already ends with a newline print(line, end = "") |
import sys import urllib.request url = "http://etc." try: infile = urllib.request.urlopen(url) except urllib.error.URLError as error: print(error, file = sys.stderr) sys.exit(1) #lines is a list of sequences of bytes. lines = infile.readlines() infile.close() try: #lines is now a list of strings of characters. lines = [line.decode("utf-8") for line in lines] except UnicodeError as error: print(error, file = sys.stderr) sys.exit(1) for line in lines: #end="" because each line already ends with a newline print(line, end = "") |