I am in the process of trying to write a python script that will take input from a CSV file and then push it into a dictionary format (I am using Python 3.x).
I use the code below to read in the CSV file and that works:
But now I want to place the results into a dictionary. I would like the first row of the CSV file to be used as the 'key' field for the dictionary with the subsequent rows in the CSV file filling out the data portion.
Sample Data:
There are additional things I would like to do with this code but for now just getting the dictionary to work is what I am looking for.
Can anyone help me with this?
EDITED Version 2: No man's sky mods xbox one.
Community♦
gakar06gakar06
4 Answers
Create a dictionary, then iterate over the result and stuff the rows in the dictionary. Note that if you encounter a row with a duplicate date, you will have to decide what to do (raise an exception, replace the previous row, discard the later row, etc..)
Here's test.csv:
and the corresponding program:
yields:
or, with DictReader:
results in:
Or perhaps you want to map the column headings to a list of values for that column:
That yields:
68 rows Tag:Odd. From Starbounder - Starbound Wiki. Jump to: navigation, search. Odd is an object. Aug 27, 2016 Forums Starbound Starbound Discussion Starbound FAQs, Q&A. Our new game Wargroove has released! Check out the forums or wiki for more information! Dismiss Notice 'place 3 wierd things in a room' quest. Discussion in 'Starbound FAQs, Q&A, and General Help' started by Lintton. Unless im mistaken 'weird things' refear to anything with. Starbound 3 weird things.
Phil FrostPhil Frost
You need a Python DictReader class. More help can be found from here
Aamir AdnanAamir Adnan26.8k1515 gold badges9191 silver badges138138 bronze badges
Help from @phil-frost was very helpful, was exactly what I was looking for.
I have made few tweaks after that so I'm would like to share it here:
You can call it:
Python Dictionary Example To Csv
Where ref_colum will be your main key for each row.
Pablo Daniel Estigarribia DavyPablo Daniel Estigarribia Davy
Have you considered using Apache Solr? It supports search scoring and easily consumes CSV file data. You'll discover it scales impressively and has lots of other options for analysing your data, for example support for multiple languages or mis-spelled queries.
Examples
Community♦
Mark O'ConnorMark O'Connor67.1k88 gold badges118118 silver badges162162 bronze badges
Not the answer you're looking for? Browse other questions tagged pythoncsvdictionary or ask your own question.
I have the following csv file(total
20000
lines)I want to convert this into a dictionary like
What i have tried
The output am getting is consisting of only few dictionaries. Like from
The whole csv file is not getting converted to dictionary.
Arafangion8,20911 gold badge2929 silver badges6666 bronze badges
MikasaMikasa
6 Answers
TonyTony
this should do what you want:
zip(*it)
will just transpose the data you have and rearrange it in the way you want; the dictionary comprehension then builds your new dictionary.1 Bedroom Condo Unit with Balcony facing the beautiful sunset of Manila Bay. Sunset View Condo @ Shell Residences. Naomi User Profile. Find the best offers for your search condominium for rent sunset view pasay. For rent: with. 250k Dp Move In Agad Rent To Own Condo In Pasay City Nr Moa. A 2br condo with sunset view. Avagail User Profile. Beautiful view of the city and ocean, friendly staff. Close to markets and restaurants.
Phone, +63 2 833 9119 Address. 2230 Roxas Boulevard; Pasay City, Philippines. Local Business Pasay City, Philippines. Apartment & Condo Building.
dct
now contains the dictionary you want.Python Read Csv To Dictionary
24k88 gold badges5050 silver badges7373 bronze badges
Awk version
quick and dirty,not memory optimized (2000 lines is not so huge form modern memory space)
NeronLeVeluNeronLeVelu8,87611 gold badge1919 silver badges3838 bronze badges
Alpesh ValakiAlpesh Valaki
chfwchfw3,06122 gold badges1919 silver badges2525 bronze badges
Ed MortonEd Morton121k1313 gold badges4646 silver badges106106 bronze badges