2017-03-07 55 views
-3

聲明我現在有存儲在一個CSV文件中這個CSV數據:如何,如果在Python

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循環」,這將只顯示我這樣的:

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

您的決定標準不明確; 「拿出意思是」沒有道理。你爲什麼要這四條線,而不是其他12條線?你在寫** ** if語句的嘗試在哪裏? – Prune

+0

爲什麼在那裏有一個'eval'? –

回答

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) 
+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