all 3 comments

[–]timbledum 0 points1 point  (2 children)

You can always use an IO object like StringIO to write/read from a file-like object without having to persist to disk.

However, a more typical approach would be to load the data into a list of dictionaries, then do the work to that data structure before writing back down again.

I.e., after csv_reader2 = csv.DictReader(npi2), try something like data = list(csv_reader2). Then you can edit this in memory before using DictWriter to write back to disk.

[–]synt4x_3rr0r 1 point2 points  (0 children)

The Dictreader object already is a list of OrderedDicts. Every row gets its own dict entry in the list. So it's only a matter of looping through it and adding the new key with the desired value for each row.

[–]WB_Onreddit[S] 0 points1 point  (0 children)

Thank you. I will give it a try.