I have a test file that I would like to load the create a list of tuples using the data on the file. The data on the file is as follows> how would I successfully load the fi
A very straightforward way would be to use the csv
module. E.g.:
import csv
filename = "input.csv"
with open(filename, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
print(row)
Use the csv module to parse the file:
import csv
output = []
with open('input_file') as in_file:
csv_reader = csv.reader(in_file)
for row in csv_reader:
output.append(tuple(row))
print output
This returns a list of tuples, each tuple corresponding to every line in the input file.
[('ocean', '4'), ('-500', ' -360'), ('-500', ' 360'), ('500', ' 360'), ('500', '-360')]