I have CSV files with varying number of columns. The first column is always a string, the other columns are always integers.
The first column is always "name", but the other columns can have different names - so I cannot hardcode the key values for any column except for "name".
Ideally there would be some kind of syntax/function that would allow me to do !"name" to int
and then I could put something together.
Here is what I have now, it does not do the conversion to int, it just builds a dictionary.
import csv
persons = []
database_csv = sys.argv[1]
with open(database_csv, "r") as database:
read_csv = csv.DictReader(database)
for row in read_csv:
persons.append(row)
I have found solutions to convert all to int, key (for example "name") to int, but I could not find anything that would let me convert everything except "name" to int.
Please note that I am using a dictionary, not a list. Solutions for lists are not directly useful downstream. If no viable solution for selectively changing datatypes within a dictionary without resorting to pandas exist then I will need to reconsider the dictionary approach and figure out how to make the rest of the code work.
I dont know if you heard about pandas. But you can used the following
import pandas as pd
df = pd.read_csv(database_csv)
persons = df.values.tolist()
Hope this helps,
import csv
persons = []
with open("database.csv", "r") as database:
read_csv = csv.DictReader(database)
for row in read_csv:
# Type decl does nothing.
row: dict
print(row.keys())
for key in row.keys():
print(row[key])
Does this work?
import csv
with open(database_csv) as fp:
csvreader = csv.reader(fp)
headers = next(csvreader)
name_idx = headers.index('name')
rows = [row for row in csvreader]
rows = [int(value) for value in row for row in rows if row.index(value) != name_idx]
To convert every value in a dict, except that for one certain key, you could do
converted = {k: (v if k == 'name' else int(v)) for k, v in row.items()}
Let's pick this apart:
{k: v for k, v in... }
— This is a dict comprehension , which lets us iterate over something and build a dict from the result. In this case, we expect the... to be a sequence of pairs, which we pick apart into two variables with the k, v
construct.v if k=='name' else int(v)
— we use a conditional expression to decide, for each item, what to do with it — keep it as-is or call int()
on it. row.items())
— this gives us each element of the row
dict as a (key, value) pair, which is what we said in the first point that we were expecting. (If you have several keys that you wish to keep unchanged, you could do
excluded = set(['name', 'address', 'preferred_colour'])
and then use if k in excluded_set
instead of if k=='name'
) )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.