[英]How to filter rows between two dates from CSV file using python and redirect to another file?
I am newbie to Python.我是 Python 的新手。 I have a CSV file with below data as an example.
我有一个包含以下数据的 CSV 文件作为示例。 I wanted to skip rows between specific date range(2018-08-01 to 2018-08-28) and redirect output to a separate CSV file.
我想跳过特定日期范围(2018-08-01 到 2018-08-28)之间的行并将输出重定向到单独的 CSV 文件。 Please note, a blank space in the header "LAST USE".
请注意,标题“LAST USE”中有一个空格。
NUMBER,MAIL,COMMENT,COUNT,LAST USE,PERCENTAGE,TEXTN
343,user1@example.com,"My comment","21577",2018-08-06,80.436%,
222,user2@example.com,"My comment","31181",2018-07-20,11.858%,
103,user3@example.com,"My comment",540,2018-06-14,2.013%,
341,user4@example.com,"My comment",0,N/A,0.000%,
Any idea would be greatly appreciated.任何想法将不胜感激。
With Pandas, this is straightforward:对于 Pandas,这很简单:
import pandas as pd
# read file
df = pd.read_csv('file.csv')
# convert to datetime
df['LAST USE'] = pd.to_datetime(df['LAST USE'])
# calculate mask
mask = df['LAST USE'].between('2018-08-01', '2018-08-28')
# output masked dataframes
df[~mask].to_csv('out1.csv', index=False)
df[mask].to_csv('out2.csv', index=False)
You can also combine Boolean arrays to construct mask
.您还可以组合布尔数组来构造
mask
。 For example:例如:
m1 = df['LAST USE'] >= (pd.to_datetime('now') - pd.DateOffset(days=30))
m2 = df['LAST USE'] <= pd.to_datetime('now')
mask = m1 & m2
dict reader documentation: https://docs.python.org/3/library/csv.html#csv.DictReader dict 阅读器文档: https : //docs.python.org/3/library/csv.html#csv.DictReader
Strptime documentation: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior strptime 文档: https ://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
Basically, we start by opening the CSV file as a set of python dictionaries - one for each row, then iterate over all the rows in the CSV.基本上,我们首先将 CSV 文件作为一组 Python 字典打开 - 每行一个,然后遍历 CSV 中的所有行。
For each row, we convert the date/time string to an actual date/time object that python can then compare with your date range.对于每一行,我们将日期/时间字符串转换为实际的日期/时间对象,python 然后可以将其与您的日期范围进行比较。 If the value is within the range, we will write the entire row to a separate CSV file.
如果该值在范围内,我们会将整行写入单独的 CSV 文件。
import datetime, csv
#define all the fieldnames in the input CSV file (for use in creating / appending to output CSV file)
fieldnames = ['NUMBER','MAIL','COMMENT','COUNT','LAST USE','PERCENTAGE','TEXTN']
#open input CSV file as readonly
with open("input.csv", "r") as fin:
#create a CSV dictionary reader object
csv_dreader = csv.DictReader(fin)
#iterate over all rows in CSV dict reader
for row in csv_dreader:
#check for invalid Date values
if 'N/A' not in row['LAST USE']:
#convert date string to a date object
datetime_val = datetime.datetime.strptime(row['LAST USE'], '%Y-%m-%d')
#check if date falls within requested range
if datetime_val > datetime.datetime(2018, 8, 1) and datetime_val < datetime.datetime(2018, 8, 28):
#if it does, open output CSV file for appending
with open("output.csv", "a") as fout:
#create a csv writer object using the fieldnames defined above
csv_writer = csv.DictWriter(fout, fieldnames=fieldnames)
#write the current row (from the input CSV) to the output CSV file
csv_writer.writerow(row)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.