# read and print each line of a plain text file in_file = open("states.txt", "r") for line in in_file: print(line) in_file.close() # read and print each line of a plain text file, eliminate extra new line in_file = open("states.txt", "r") for line in in_file: print(line, end='') in_file.close() # write data to plain text file out_file = open("gettysburg.txt", "w") out_file.write("Four score and seven years ago,\n") out_file.write("our fathers brought forth on this continent, a new nation,\n") out_file.write("conceived in Liberty, and dedicated to the proposition ") out_file.write("that all men are created equal.\n") out_file.close() # read csv file import csv csvfile = open('us-500.csv', 'r') csvdata = csv.reader(csvfile) for row in csvdata: print( row ) csvfile.close() # read csv with different file open/close syntax import csv with open( 'us-500.csv' ) as csvfile: csvdata = csv.reader(csvfile) for row in csvdata: print( row ) # read csv file and search for specific data import csv with open( 'us-500.csv') as csvfile: csvdata = csv.reader(csvfile) next(csvdata, None) # skip the header for row in csvdata: if( row[7] == '94104' ): print( row[0], row[1], row[7] ) # print name/zip