使用Python在CSV文件中的列中搜索值

时间:2018-04-14 02:34:52

标签: python

我需要在CSV文件中逐行扫描时间列,然后查看5小时前其他列中是否有活动,然后添加值为1的列。

这是我的想法:

import csv
from collections import namedtuple
from contextlib import closing
light3 = pd.read_csv('G:/light3.csv')
light3.Time = pd.to_datetime(light3.Time)

m = light3.Time - DateOffset(hours = 5)
def search():
    item = light3[(light3['Time']> m)| (light3['Time']== m)]
    raw_data = 'G:/light3.csv'

    failure = 'No matching item could be found with that item code. Please try again.'
    check = False
with open('G:/project/test.pcap_z/light.csv','r') as csvinput:
    with open('G:/light1.csv', 'w') as csvoutput:
        writer = csv.writer(csvoutput, lineterminator='\n')
        reader = csv.reader(csvinput)

        read_data = csv.DictReader(csvinput, delimiter=';')
        item_data = namedtuple(read_data['Time'], read_data.Time)
        all = [ ]
        row = next(reader)
        row.append('f')
        all.append(row)
        while check == False:
                for row in reader:
                    if row.Item == item:
                        row.append('f')
                        all.append(row)
                        row.append(1)

        writer.writerows(all)    

0 个答案:

没有答案