- 443
- 2
- 5
- 12
I have a bunch of .csv files which I have to read and look for data. The .csv file is of the format:
A row of data I will ignore State,County,City WA,king,seattle WA,pierce,tacoma
In every csv file, the order of columns is not consistent. For example in csv1 the order can be State,County,City, in csv2 it can be City,County,State. What I am interested is the State and County. Given a county I want to find out what State it is in. I am ignoring the fact that same counties can exist in multiple States. The way I am approaching this:
with open(‘file.csv’) as f:
data = f.read()
# convert the data =to re.sub(r"^.*\n",""iterable, data)skip #Removethe first line
io = StringIO(data)
reader = csv.DictReader(iodata.splitlines(1)[1:])
lines = list(reader)
counties = {k: v for (k,v in ((line[‘county’], line[‘State’]) for line in lines)}
Is there a better approach to this?
I have a bunch of .csv files which I have to read and look for data. The .csv file is of the format:
A row of data I will ignore State,County,City WA,king,seattle WA,pierce,tacoma
In every csv file, the order of columns is not consistent. For example in csv1 the order can be State,County,City, in csv2 it can be City,County,State. What I am interested is the State and County. Given a county I want to find out what State it is in. I am ignoring the fact that same counties can exist in multiple States. The way I am approaching this:
with open(‘file.csv’) as f:
data = f.read()
data = re.sub(r"^.*\n","", data) #Remove first line
io = StringIO(data)
reader = csv.DictReader(io)
lines = list(reader)
counties = {k: v for (k,v in ((line[‘county’], line[‘State’]) for line in lines)}
Is there a better approach to this?
I have a bunch of .csv files which I have to read and look for data. The .csv file is of the format:
A row of data I will ignore State,County,City WA,king,seattle WA,pierce,tacoma
In every csv file, the order of columns is not consistent. For example in csv1 the order can be State,County,City, in csv2 it can be City,County,State. What I am interested is the State and County. Given a county I want to find out what State it is in. I am ignoring the fact that same counties can exist in multiple States. The way I am approaching this:
with open(‘file.csv’) as f:
data = f.read()
# convert the data to iterable, skip the first line
reader = csv.DictReader(data.splitlines(1)[1:])
lines = list(reader)
counties = {k: v for (k,v in ((line[‘county’], line[‘State’]) for line in lines)}
Is there a better approach to this?
I have a bunch of csv.csv files which I have to read and look for data. The csv.csv file is of the format:
A row of data I will ignore
State,County,City
WA,king,seattle
WA,pierce,tacoma
A row of data I will ignore State,County,City WA,king,seattle WA,pierce,tacoma
In every csv file, the order of columns is not consistent. For example in csv1 the order can be State,County,City, in csv2 it can be City,County,State. What I am interested is the State and County. Given a county I want to find out what State it is in. I am ignoring the fact that same counties can exist in multiple States. The way I am approaching this:
with open(‘file.csv’) as f:
data = f.read()
data = re.sub(r"^.*\n","", data) #Remove first line
io = StringIO(data)
reader = csv.DictReader(io)
lines = list(reader)
counties = {k: v for (k,v in ((line[‘county’], line[‘State’]) for line in lines)}
with open(‘file.csv’) as f:
data = f.read()
data = re.sub(r"^.*\n","", data) #Remove first line
io = StringIO(data)
reader = csv.DictReader(io)
lines = list(reader)
counties = {k: v for (k,v in ((line[‘county’], line[‘State’]) for line in lines)}
Is there a better approach to this?
I have a bunch of csv files which I have to read and look for data. The csv file is of the format:
A row of data I will ignore
State,County,City
WA,king,seattle
WA,pierce,tacoma
In every csv file, the order of columns is not consistent. For example in csv1 the order can be State,County,City, in csv2 it can be City,County,State. What I am interested is the State and County. Given a county I want to find out what State it is in. I am ignoring the fact that same counties can exist in multiple States. The way I am approaching this:
with open(‘file.csv’) as f:
data = f.read()
data = re.sub(r"^.*\n","", data) #Remove first line
io = StringIO(data)
reader = csv.DictReader(io)
lines = list(reader)
counties = {k: v for (k,v in ((line[‘county’], line[‘State’]) for line in lines)}
Is there a better approach to this?
I have a bunch of .csv files which I have to read and look for data. The .csv file is of the format:
A row of data I will ignore State,County,City WA,king,seattle WA,pierce,tacoma
In every csv file, the order of columns is not consistent. For example in csv1 the order can be State,County,City, in csv2 it can be City,County,State. What I am interested is the State and County. Given a county I want to find out what State it is in. I am ignoring the fact that same counties can exist in multiple States. The way I am approaching this:
with open(‘file.csv’) as f:
data = f.read()
data = re.sub(r"^.*\n","", data) #Remove first line
io = StringIO(data)
reader = csv.DictReader(io)
lines = list(reader)
counties = {k: v for (k,v in ((line[‘county’], line[‘State’]) for line in lines)}
Is there a better approach to this?
Parse a csv file and create a dictionary of partial results
I have a bunch of csv files which I have to read and look for data. The csv file is of the format:
A row of data I will ignore
State,County,City
WA,king,seattle
WA,pierce,tacoma
In every csv file, the order of columns is not consistent. For example in csv1 the order can be State,County,City, in csv2 it can be City,County,State. What I am interested is the State and County. Given a county I want to find out what State it is in. I am ignoring the fact that same counties can exist in multiple States. The way I am approaching this:
with open(‘file.csv’) as f:
data = f.read()
data = re.sub(r"^.*\n","", data) #Remove first line
io = StringIO(data)
reader = csv.DictReader(io)
lines = list(reader)
counties = {k: v for (k,v in ((line[‘county’], line[‘State’]) for line in lines)}
Is there a better approach to this?