Reputation: 57
I am trying to search within a csv file. My problem is that the rows don't split. This is the csv file:
Country;Capital;Continent;Coastline;Desert;Population[million]
Mongolia;Ulaanbaatar;Asia;no;yes;3
Malawi;Lilongwe;Africa;no;no;18
Gabon;Libreville;Africa;yes;no;2
Japan;Tokyo;Asia;yes;no;126
China;Bejing;Asia;yes;yes;1404
Namibia;Windhoek;Africa;yes;yes;3
This is the program so far:
import csv
def get_info(file, Continent=None, Coastline=None, Desert=None):
result = []
with open(file) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
for row in csv_reader:
print(row)
if row[2] == Continent and row[3] == Coastline and row[4] == Desert:
result.append(row[0])
result.append(row[5])
return result
But i get this error:
IndexError: list index out of range
Upvotes: 0
Views: 715
Reputation: 31
Seems to work for me. Make sure you are calling the function with the correct values. Here is how I called it. I assumed that the date file is 'geo.csv')
result = get_info("geo.csv", "Asia", "yes", "yes")
print(result)
# got the output ['China', '1404']
Just a comment on your code. It is bad form to use Title case (first letter capitalized) for variable names. Title case is for classes in python.
Following up on Yuri's suggestion, you can use simple file object methods to achieve the same unless the objective of this exercise is to familiarize you with the csv module. Here is that code:
def get_info1(file, continent, coastline, desert):
with open("geo.csv") as f:
result = []
geo_data = f.readlines() #reads entire file into a list
for row in geo_data:
row = row.strip().split(";")
if (row[2] == continent and row[3] == coastline and row[4] == desert):
result.append(row[0])
result.append(row[-1])
return result
Upvotes: 2
Reputation: 14502
It could be something like this:
csv = '''Country;Capital;Continent;Coastline;Desert;Population[million]
Mongolia;Ulaanbaatar;Asia;no;yes;3
Malawi;Lilongwe;Africa;no;no;18
Gabon;Libreville;Africa;yes;no;2
Japan;Tokyo;Asia;yes;no;126
China;Bejing;Asia;yes;yes;1404
Namibia;Windhoek;Africa;yes;yes;3'''
parsed_csv = [line.split(";") for line in csv.split()]
def get_info(data, Continent=None, Coastline=None, Desert=None):
result = []
for row in data:
if row[2] == Continent and row[3] == Coastline and row[4] == Desert:
result.append(row[0])
result.append(row[5])
return result
print(get_info(parsed_csv, "Asia", "no", "yes")) # output ['Mongolia', '3']
To parse csv-like data you need 2 step:
Step 1: split data by lines
Step 2: split every line by delimiter
In python it can be done like this:
# here is your csv data "abc,def..."
csv_data = "..."
# Step 1. Here you make lines [[abc],[def]...]
lines = csv_data.split()
# Step 2. here you split every line by some delimiter
# and combine them in 2D-list [[a,b,c][d,e,f]...]
parsed_data = [row.split(delimiter) in row for lines]
Both the steps can be done all at once in one line:
parsed_data = [row.split(delimiter) in row for data.split()]
Can you figure out how to get the content from your cvs file?
Upvotes: 1
Reputation: 127
You are getting IndexError
because the entire data in csv file is located in 1 column.
Renaming your dataset as .txt
file does solve your issue.
Or if your dataset is small you can manually split values into separate columns. Remove delimiter if you are splitting.
Upvotes: 0