2016-05-31 66 views
1

我正在嘗試逐行讀取csv文件。但事實證明,首先預計會有點困難。如何讀取csv文件,單獨進行進一步處理

驗證碼:

#!/usr/bin/env python 

import glob 
import os 

file_dir=os.getcwd() 
files_2=glob.glob(file_dir) 

m_class="ABC" 
m_id="123" 
device=m_class+"-"+m_id 

with open('output_temp.csv', 'w') as output_temp: 
    for filename in glob.glob(os.path.join(os.getcwd(), device+'*.log')): 
     #print "----\nFILE NAME: " + filename 
     with open(filename, 'r') as f: 
      content = f.readlines() 
      if content: 
       mycontent=str("".join(content)) 
       #print mycontent 
       output_temp.write(str(mycontent)) 

counter=1 
with open('output_temp.csv', 'r') as f: 
    content = f.readlines() 
    print "Line: " + str(counter) + " data: " + str(content) 
    counter=counter+1 

output_temp.csv文件:

1464557866.4111354 
1464561244.9223452 
1464506206.4268115 
1464507324.3743494 
1464491791.4750218 
1464492017.1200309 
1464560723.4278536 
1464560838.5569682 
1464578213.2567956 
1464580860.4225895 
1464534128.2530715 
1464545504.5457716 
1464603405.5002685 
1464610938.5988958 
1464560390.4099076 
1464579888.7111971 
1464591413.4147444 
1464595286.6162462 
1464548255.5633001 
1464548400.8739398 
1464596402.442414 
1464603613.2534776 
1464523008.4462287 
1464524100.9739816 
1464389395.6676936 
1464389586.7012687 
1464542283.2585688 
1464548192.5785992 

的端子輸出:

Line: 1 data: ['1464557866.4111354\n', '1464561244.9223452\n', '1464506206.4268115\n', '1464507324.3743494\n', '1464491791.4750218\n', '1464492017.1200309\n', '1464560723.4278536\n', '1464560838.5569682\n', '1464578213.2567956\n', '1464580860.4225895\n', '1464534128.2530715\n', '1464545504.5457716\n', '1464603405.5002685\n', '1464610938.5988958\n', '1464560390.4099076\n', '1464579888.7111971\n', '1464591413.4147444\n', '1464595286.6162462\n', '1464548255.5633001\n', '1464548400.8739398\n', '1464596402.442414\n', '1464603613.2534776\n', '1464523008.4462287\n', '1464524100.9739816\n', '1464389395.6676936\n', '1464389586.7012687\n', '1464542283.2585688\n', '1464548192.5785992\n'] 

問:我想單獨在csv文件中打印每一行,以便對值進行一些操作,附加一些值並進行一些連接。如何單獨打印每條線,如output_temp.csv文件

注意:我是Python新手。

回答

0

的這裏的問題是這段代碼:

counter=1 
with open('output_temp.csv', 'r') as f: 
    content = f.readlines() 
    print "Line: " + str(counter) + " data: " + str(content) 
    counter=counter+1 

content這裏是所有行的列表。通過換行符的content.strip()數據迭代和你應該罰款:

# No need a counter 
with open('output_temp.csv', 'r') as f: 
    content = f.readlines() 
    for count, data in enumerate(content): 
     print "Line: " + str(count+1) + " data: " + data.strip("\n") 

輸出:

Line: 1 data: 1464557866.4111354 
Line: 2 data: 1464561244.9223452 
Line: 3 data: 1464506206.4268115 
Line: 4 data: 1464507324.3743494 
Line: 5 data: 1464491791.4750218 
Line: 6 data: 1464492017.1200309 
Line: 7 data: 1464560723.4278536 
Line: 8 data: 1464560838.5569682 
Line: 9 data: 1464578213.2567956 
Line: 10 data: 1464580860.4225895 
Line: 11 data: 1464534128.2530715 
Line: 12 data: 1464545504.5457716 
Line: 13 data: 1464603405.5002685 
Line: 14 data: 1464610938.5988958 
Line: 15 data: 1464560390.4099076 
Line: 16 data: 1464579888.7111971 
Line: 17 data: 1464591413.4147444 
Line: 18 data: 1464595286.6162462 
Line: 19 data: 1464548255.5633001 
Line: 20 data: 1464548400.8739398 
Line: 21 data: 1464596402.442414 
Line: 22 data: 1464603613.2534776 
Line: 23 data: 1464523008.4462287 
Line: 24 data: 1464524100.9739816 
Line: 25 data: 1464389395.6676936 
Line: 26 data: 1464389586.7012687 
Line: 27 data: 1464542283.2585688 
Line: 28 data: 1464548192.5785992 

看來您不熟悉Python中循環的基礎。請閱讀here以及更多關於enumeratehere的更多信息。請記住,如果有疑問,Google或Pydoc!

+0

謝謝你,但我怎麼能從str(data.strip(「\ n」))'減去一個?例如'prev = 1464557866.4111354 - 1.0' – 3kstc

+0

@ 3kstc您可以將其轉換爲'float',然後返回到'str'。例如'str(float(data.strip(「\ n」)) - 1.0)' –

0

在Python中使用csv模塊,你可以很容易地做到這一點。下面的示例逐行顯示csv文件。

import csv 
import os 

os.chdir("/Users/xaviermerino/Desktop") 

with open('csvExample.csv') as csvFile: 
    reader = csv.reader(csvFile) 
    for row in reader: 
     print row[0] 
相關問題