简体   繁体   English

如何使用python从CSV文件中过滤两个日期之间的行并重定向到另一个文件?

[英]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.

相关问题 如何根据该文件中行中的日期将 CSV 文件在特定日期之间的值传输到另一个 CSV 文件? - How do I transfer values of a CSV files between certain dates to another CSV file based on the dates in the rows in that file? Python-如何使用CSV文件中的循环查找同一列的两行之间的差异? - Python - How can I find difference between two rows of same column using loop in CSV file? 如何使用 Python 将行从一个 CSV 复制到另一个 CSV 文件 - How to copy rows from one CSV to another CSV file using Python 使用 python 编程在 csv 文件中搜索两个日期和两次之间的记录 - searching for records between two dates and two times in csv file using python programming 如何使用 Python 脚本从巨大的 CSV 文件中过滤特定行 - How to Filter specific rows from a huge CSV file using Python Script 如何使用python基于阈值数值从csv文件过滤行 - How to filter rows from a csv file based on threshold numerical values using python 在 Python 中比较 csv 文件中的两行 - Comparing two rows from a csv file in Python 如何使用来自另一个 .txt 的列表过滤 .csv/.txt 文件 - how to filter a .csv/.txt file using a list from another .txt 如何使用Python脚本从CSV文件过滤 - How to Filter from CSV file using Python Script 在Python中将特定行从csv文件写入另一个csv - Writing a specific rows from csv file to another csv in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM