0

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.

  • 2
    why not using the `csv` module? it's designed for that specifically... – Jean-François Fabre Sep 25 '17 at 09:00
  • I've never encountered that module before, so a reply on how to use it properly with my scenario would be better than locking my thread from replies and giving me an alternate link which was an absolute waste of time. – Nathan Rose Sep 25 '17 at 09:24
  • nope, it's a waste of time for me doing your code. You have the doc, examples, try it yourself. SO isn't a code writing service. If someone disagrees, he'll reopen the question. – Jean-François Fabre Sep 25 '17 at 09:26
  • I have been trying it myself - you gave me examples which have very little relevancy to my question and they didn't help at all. I'm aware Stack Overflow isn't a place to get code wrote for you, but I'm requesting assistance and you've failed to provide. – Nathan Rose Sep 25 '17 at 09:29
  • your question is way too basic. We're here to answer _good_ questions, not help individuals who don't have time to read the documentation or tutorials – Jean-François Fabre Sep 25 '17 at 09:30
  • in your case `csvfile = open ("1000people.csv"); cr = csv.reader(csvfile); l = list(cr)` here you go for the spoonfeeding. – Jean-François Fabre Sep 25 '17 at 09:31
  • I appreciate your help, however, I'm confused as to whether this'll give me the ability to target specific columns and print them when desired - as of now, it's printing the whole csv file 10 times instead of the first ten lines. I apologize if I'm doing something wrong, but it may be due to my basic knowledge of Python. – Nathan Rose Sep 25 '17 at 09:38

1 Answers1

1

the csv module helps to split the columns. A pythonic way to achieve what you want would be:

import csv
with open("1000people.csv") as f:
    cr = csv.reader(f)  # default sep is comma, no need to change it
    first_10_rows = [next(cr,[]) for _ in range(10)]

the next(cr,[]) instruction consumes one row in a list comprehension, and if the file is smaller than 10 rows, you'll get an empty row instead of an exception (that's the purpose of the second argument)

now first_10_rows is a list of lists containing your values. Quotes & commas are properly stripped off thanks to the csv module.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219