'generating a different DictReader object from an existing Dictreader object [duplicate]
I could really use some help here:
I import a csv file to python using the csv.DictReader() method and the data structure looks like this:
{'first_name': 'John', 'last_name': 'Doe', 'email': '[email protected]'} {'first_name': 'Mary', 'last_name': 'Jane', 'email': '[email protected]'} {'first_name': 'John', 'last_name': 'Smith', 'email': '[email protected]'} {'first_name': 'Vera', 'last_name': 'Wang', 'email': '[email protected]'}
The result I want to have is to return another DictReader object in the shape of:
{'first_name': 'John', 'last_name': 'Doe'}` {'first_name': 'Mary', 'last_name': 'Jane'} {'first_name': 'John', 'last_name': 'Smith'} {'first_name': 'Vera', 'last_name': 'Wang'}
i.e., dropping the last key of 'email';
While I do know using the del DictReader_Object['email'] can do the job, but is there a way to select multiple keys/columns?
e.g., I have 10s of columns in the dataset but I only need two of the columns; if using del function I will have to write the other 8 columns; so I just want a way to select two cols directly.
Thanks for the help!
Solution 1:[1]
Edit: found a dupe afterwards, leaving answer as community edit in non the less.
You can create smaller dicts while reading from your DictReader, put them inside a list and use that in a similar fashion:
from csv import DictReader, DictWriter
# create data
data = []
fn = list("abcdef")
for k in fn:
data.append([])
for v in range(4):
data[-1].append( (k,v))
# print(data, "\n")
# transpose
data = zip(*data)
dd = [dict(w) for w in data]
print(dd , "\n")
# write big dict file
with open("k.dict","w", newline="") as f:
w = DictWriter(f, fieldnames=fn)
w.writeheader()
w.writerows(dd)
print(open("k.dict").read(), "\n")
# get small dicts from file
whatIwant = ("c","d")
result = []
with open("k.dict") as f:
w = DictReader(f)
for l in w:
result.append( dict( (k,v) for k,v in l.items() if k in whatIwant))
print(result)
Output:
# data transposed as dict's
[{'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0},
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1},
{'a': 2, 'b': 2, 'c': 2, 'd': 2, 'e': 2, 'f': 2},
{'a': 3, 'b': 3, 'c': 3, 'd': 3, 'e': 3, 'f': 3}]
# file content
a,b,c,d,e,f
0,0,0,0,0,0
1,1,1,1,1,1
2,2,2,2,2,2
3,3,3,3,3,3
4,4,4,4,4,4
# list of minified dicts after reading
[{'c': '0', 'd': '0'}, {'c': '1', 'd': '1'},
{'c': '2', 'd': '2'}, {'c': '3', 'd': '3'}]
Edit: You can find more ways to do similar things in Python CSV DictReader ignore columns?
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 |
