CSV file on Python #21498
-
Hello, I have to write a program which processes a csv file with all the names given in France since the 19th century. I want to separate the list in two. In the first, I want to put the boy’s name and in the second, I want to put the girl’s name. I tried many things but they have not worked. Could someone help me to find a solution please ? Thanks in advance
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
No one can help you if you don’t provide a portion of the CSV file so we can tell what you are trying to read. Also, you don’t tell us how your program “fails”. |
Beta Was this translation helpful? Give feedback.
-
The program is executed but it puts all the names in the male and female list as if there were no conditions. And so I end up with 2 lists containing all the names. Maybe I can convert the first and last character to an integer but I don’t know how to take it. |
Beta Was this translation helpful? Give feedback.
-
You want to read about using csv.DictReader instead of csv.reader. With DictReader you will get a dictionary which is indexed based on the column names in the first line of the file. Then you will be able to do things like:
You can’t do that with the regular csv.reader. Your “csv” file is actually using a semi-colon as the delimiter. It may be necessary to let the csv package know this, by including
as a parameter to the csv.DictReader call. |
Beta Was this translation helpful? Give feedback.
-
That work. Thank you |
Beta Was this translation helpful? Give feedback.
You want to read about using csv.DictReader instead of csv.reader.
With DictReader you will get a dictionary which is indexed based on the column names in the first line of the file. Then you will be able to do things like:
You can’t do that with the regular csv.reader.
Your “csv” file is actually using a semi-colon as the delimiter. It may be necessary to let the csv package know this, by including
as a parameter to the csv.DictReader call.