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,aebbings7z@diigo.com,Male,23/06/1993
Abby,Powney,apowneynj@walmart.com,Female,01/03/1998
Abbye,Cardus,acardusji@ft.com,Female,30/10/1960
Abeu,Chaize,achaizehi@apple.com,Male,25/06/1994
Abrahan,Shuard,ashuardb5@zdnet.com,Male,09/12/1995
Adah,Lambkin,alambkinga@skyrock.com,Female,21/08/1992
Addison,Shiers,ashiersmg@freewebs.com,Male,13/07/1981
Adelaida,Sheed,asheedqh@elpais.com,Female,06/08/1976
Adelbert,Jurkowski,ajurkowski66@amazonaws.com,Male,27/10/1957
Adelice,Van Arsdall,avanarsdallqp@pagesperso-orange.fr,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.