简体   繁体   中英

Printing only certain rows in a .csv file

I have a .csv file that contains a date of birth field similar to this:

John,Smith,34 La La Lane,14/03/85,johnsmith@email.com
Sarah,Second,42 Wallaby Way,11/06/92,sarahsecond@email.com
Third,Example,99 Peacock Terrace,04/12/89,thirdexample@email.com

And I want to make a program that only prints rows in the file of entries born during a certain month (in this case the month is after the first slash, ie. dd/mm/yy).

So, if the desired month was March, it'd print off John Smith's entry.

Any help on this would be great, I've been struggling for a while

I'm not sure which part of the problem you're struggling with, so I'll give a somewhat general answer. Python has a csv reader you can use like this:

import csv
desiredMonth = 3
with open('people.csv', 'rb') as csvfile:
    content = csv.reader(csvfile, delimiter=',')
    for row in content:
        month = int(row[3].split('/')[1])
        if month == desiredMonth:
            # print the row or store it in a list for later printing

row will already be separated out for you into a list, so row[3] will be the birthday. split() then separates the month portion into pieces, and [1] gives the second piece, which is the month. Converting it to int is a good idea so you can easily compare it to whatever month you want.

Here's a different approach...For working with csv files, the python package csvkit installs a number of command-line utilities that let you slice and dice your .csv files really easily.

$ pip install csvkit

This will install a command called csvgrep (among others).

$ csvgrep -c 4 -r '\d{2}/03' yourfile.csv
First,Last,Address,Birthdate,Email
John,Smith,34 La La Lane,14/03/85,johnsmith@email.com

One thing to note is that the csvkit assumes all .csv files have header rows. That's why the result of the csvgrep shows a header row. That also means that you will have to add a header to your data file like this:

First,Last,Address,Birthdate,Email
John,Smith,34 La La Lane,14/03/85,johnsmith@email.com
Sarah,Second,42 Wallaby Way,11/06/92,sarahsecond@email.com
Third,Example,99 Peacock Terrace,04/12/89,thirdexample@email.com 

Explanation of command-line args:

$ csvgrep -c 4 -r '\d{2}/03' yourfile.csv
-c specifies which column you want to search 
-r specifies the regular expression you want to match in the column

The regex '^\\d{2}/03' will match a string that starts with 2 digits, then a '/', then the month '03'.

Check out the csvkit tutorial for more info.

import csv
with open('yourfile.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',')
    for row in spamreader:
        date = row[3]
        month = date.split('/')[1]
        if int(month) >= YOUR_MONTH_HERE
            print row

As much tutorial type as I could put into it :-)

somecsvfile=r'/home/me/Desktop/txt.csv'
the_month_you_are_looking_for = 6 # as in june.
with open(somecsvfile, 'r') as fi:
    for line in fi:   
        list_from_text = line.split(',')
        bday = list_from_text[3]
        bmonth = int(bday.split('/')[1])
        if bmonth == the_month_you_are_looking_for:
            print (line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM