2015-04-25 35 views
-2

我目前正在試圖建立一個程序,讀取一個CSV文件,更具體的行數據(跨線數據)讀行的數據文件

Sampledata.csv:

['Time', 'Date', 'Color ', 'Name'] 
['1pm', '01-01-99', 'blue', 'jack'] 
['2pm', '02-02-99', 'green', 'kevin'] 
['3pm', '03-03-99', 'yellow', 'phil'] 
['4pm', '04-04-99', 'white', 'alice'] 
['5pm', '05-05-99', 'black', 'bob'] 

這是我的代碼:

import csv 
with open('Sampledata.csv', 'r') as csvfile : 
     regnumber = input("What is your regnumber?") 
     reader = csv.reader(csvfile, delimiter=',') 
for row in reader: 
    print(row) # here lies the problem(python is reading columnal data (data going down) instead of row(lines across) data# 

問題是在讀取列(數據往下)。 Python改爲讀取列。

輸出:

Date 
01-01-99 
02-02-99 
03-03-99 
04-04-99 
05-05-99 
+1

那麼有什麼問題嗎? – nu11p01n73R

+0

您的代碼(即使使用正確的縮進)和輸出不匹配。當我運行它時,我得到正確的輸出,即每行的數據。 – user38034

回答

1

這是你要找的人嗎?

import csv #ignore my stupidity with the indentation and spaces# 
with open('Sampledata.csv', 'r') as csvfile : 
    regnumber = raw_input("Enter the time:") 
    reader = csv.reader(csvfile) 
    for row in reader: 
     if(row[0]==regnumber): 
      print ', '.join(row) 
     else: 
      continue 

上面的代碼逐行顯示csv文件的值。

+0

Sree Harissh它可以工作,但它只顯示柱面數據(每列的數據)我想要行數據(線條) –

+0

@albinvarghese - 請編輯您的問題以向我們展示您實際上想看到的內容。你的描述很混亂。 –

+0

所以這是csv文件的權利:['時間','日期','顏色','名稱'] ['1pm','01 -01-99','blue','jack'] [ '2pm','02 -02-99','green','kevin'] ['3pm','03 -03-99','yellow','phil'] ['4pm','04 -04-99','white','alice'] ['5pm','05 -05-99','black','bob']我想讀一行,例如如果說下午4點,我會想python閱讀下午4點線,所以輸出會是這樣的:['4pm','04-04'','white','alice'] –