[英]Printing only certain rows in a .csv file
I have a .csv file that contains a date of birth field similar to this: 我有一个.csv文件,其中包含与此类似的出生日期字段:
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). 我想制作一个程序,只打印某个月内出生的条目文件中的行(在这种情况下,月份是在第一个斜杠之后,即dd / mm / yy)。
So, if the desired month was March, it'd print off John Smith's entry. 因此,如果所需的月份是3月份,它将打印出John Smith的条目。
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:
Python有一个csv阅读器,你可以像这样使用:
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. row
已经被分离出来进入列表,所以row[3]
将是生日。 split()
then separates the month portion into pieces, and [1]
gives the second piece, which is the month. split()
然后将月份部分分成几部分, [1]
给出第二部分,即月份。 Converting it to int
is a good idea so you can easily compare it to whatever month you want. 将其转换为
int
是一个好主意,因此您可以轻松地将其与您想要的任何月份进行比较。
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. 这是一个不同的方法...对于使用csv文件,python包
csvkit
安装了许多命令行实用程序,可以让您轻松切片和切块.csv文件。
$ pip install csvkit
This will install a command called csvgrep
(among others). 这将安装一个名为
csvgrep
的命令(以及其他命令)。
$ 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. 需要注意的一点是,
csvkit
假设所有.csv文件都有标题行。 That's why the result of the csvgrep
shows a header row. 这就是为什么
csvgrep
的结果显示标题行的原因。 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'. 正则表达式'^ \\ d {2} / 03'将匹配以2位开头的字符串,然后是'/',然后是月'03'。
Check out the csvkit tutorial for more info. 查看csvkit教程以获取更多信息。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.