如何在python中声明

时间:2017-03-07 17:39:30

标签: python csv

我目前将此CSV数据存储在csv文件中:

33.23,1376460060.0,01:01:00.000000
33.29,1376460060.25,01:01:00.250000
33.29,1376460060.5,01:01:00.500000
33.29,1376460060.75,01:01:00.750000
33.33,1376460061.0,01:01:01.000000
33.29,1376460061.25,01:01:01.250000
33.33,1376460061.5,01:01:01.500000
33.29,1376460061.75,01:01:01.750000
33.33,1376460062.0,01:01:02.000000
33.33,1376460062.25,01:01:02.250000
33.37,1376460062.5,01:01:02.500000
33.33,1376460062.75,01:01:02.750000
33.33,1376460063.0,01:01:03.000000
33.33,1376460063.25,01:01:03.250000
33.33,1376460063.5,01:01:03.500000
33.37,1376460063.75,01:01:03.750000

有人可以帮我创建一个" if语句"或者" while loop"这只会告诉我这个:

33.33,1376460062.0,01:01:02.000000
33.33,1376460062.25,01:01:02.250000
33.37,1376460062.5,01:01:02.500000
33.33,1376460062.75,01:01:02.750000

所以我可以取出第1栏的意思。

我正在读这样的文件:

 Input_Tiempo = raw_input("----INPUT TIME: ")

 Full_InTi = Input_Tiempo 

 #This will convert Full_InTi to time in UNIX
 Full_InTiUNIX = time.mktime(datetime.datetime.strptime("14:08:2013:"+Full_InTi, "%d:%m:%Y:%H:%M:%S.%f").timetuple())

 #reads my csv file
 DATOS = np.genfromtxt(csvname+'NEW.csv', delimiter=',', dtype=None)

 LISTA = []

 for COLUMN in DATOS:
     INTC= eval(COLUMN[1])
     INTF = float(Full_InTiUNIX)

     if INTF <= INTC:
        if INTF <= INTC:
           print "D: "+COLUMN[0]+" TU: "+COLUMN[1] +" H: " + COLUMN[2]
           LISTA.append(COLUMN[2])

到目前为止,我还没有能力,它只显示我需要的东西。

2 个答案:

答案 0 :(得分:0)

您没有向我们提供有关原始csv的足够信息(例如标题或获取该行的条件)

对于给定的数据,请尝试以下方法:

import csv

with open('your_file.csv', 'r') as my_csv_file:

    csv_data_dict = csv.DictReader(my_csv_file)
    searched_key = 'the_column_name_of_the_second_data_in_your_line'

    for line in csv_data_dict:
        if '1376460062' in line[searched_key]:
            print line

答案 1 :(得分:0)

如果您只需要1376460062。**作为第二列,那么可以执行以下操作:

import csv
with open("csvname",'r') as inp:
    row=csv.reader(inp)
    for i in row:
        if '1376460062' in i[1]:
            print(i)