'Python how to separate different values in same column in csv file?
I have a csv file that looks like this: csv file
Essentially I'm trying to add each value in column E (political regime) to a list of lists, separated by the country name in column B.
So, every time Afghanistan appears, add its political regime value to a list, then when a new country appears, add those values to another list.
I also have to do this task WITHOUT pandas or numpy.
I hope this makes enough sense but here's my current code which is an infinite loop and freezes my program.
def read_file(fp):
reader = csv.reader(fp)
next(reader, None)
country_names = []
list_of_regime_lists = []
for line in reader:
country_names.append(line[1])
for line in reader:
line[4]=int(line[4])
return country_names, list_of_regime_lists
Solution 1:[1]
This is a good use case for a dictionary.
def read_file(fp):
with open(fp, 'r') as file:
contents = file.readlines()
regimes = {}
for row in contents[1:]: # skip header row
fields = row.split(',')
country = fields[1] # column B
regime = fields[4] # column E
if country not in regimes: # check if this is a new country
regimes[country] = [regime] # start a new list of regimes
# regimes[country] = {regime} # alternatively use a set to ignore duplicate regimes
else:
regimes[country].append(regime) # or add the regime to that country's list
# regimes[country].add(regime) # if using a set
return regimes
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hbgoddard |
