I'm having difficulty completing a coding challenge and would like some help if possible.
I have a CSV file with 1000 forenames, surnames, emails, genders and dobs (date of births) and am trying to separate each column. My first challenge was to print the first 10 rows of the file, I did so using;
counter = 0
print("FORENAME SURNAME EMAIL GENDER DATEOFBIRTH","\n")
csvfile = open ("1000people.csv").read().splitlines()
for line in csvfile:
print(line+"\n")
counter = counter + 1
if counter >= 10:
break
This works and prints the 10 first rows as intended. The second challenge is to do the same, but in alphabetical order and only the names. I can only manage to print the first 10 rows alphabetically using:
counter = 0
print("FORENAME SURNAME EMAIL GENDER DATEOFBIRTH","\n")
csvfile = open ("1000people.csv").read().splitlines()
for line in sorted(csvfile):
print(line+"\n")
counter = counter + 1
if counter >= 10:
break
Outcome of above code:
>>>
FORENAME SURNAME EMAIL GENDER DATEOFBIRTH
Abba,Ebbings,[email protected],Male,23/06/1993
Abby,Powney,[email protected],Female,01/03/1998
Abbye,Cardus,[email protected],Female,30/10/1960
Abeu,Chaize,[email protected],Male,25/06/1994
Abrahan,Shuard,[email protected],Male,09/12/1995
Adah,Lambkin,[email protected],Female,21/08/1992
Addison,Shiers,[email protected],Male,13/07/1981
Adelaida,Sheed,[email protected],Female,06/08/1976
Adelbert,Jurkowski,[email protected],Male,27/10/1957
Adelice,Van Arsdall,[email protected],Female,30/06/1979
So would there be a way to separate the columns so I can print just one specific column when chosen?
Thank you for reading and replying if you do.