2012-07-11 26 views
0

我想在Python3中完成一項任務。這是非常相似的pdf找到here從.csv文件獲取數據,分析,產生輸出 - python3

我對如何獲得我需要的信息的執行有幾個問題,如果可能的話,一些代碼可以移動我。我是python的新手。就像我現在的代碼一樣,在運行一個函數來嘗試讀取數據之後,我總是收到錯誤"directory not found"。我知道.csv文件應該位於我將其保存到WingIDE中的目錄中,但我無法使其正常工作。

我的第一個問題是獲得.csv文件中的每一行從我get_file_list閱讀後,什麼是採取每個類別,把它扔進了效率方程式的最佳方式是什麼?

這裏是我的get_data_list功能:

def get_data_list(filename): 
    data_file = open(filename, "r") 
    data_list = [ ] 
    for line_str in data_file: 
     data_list.append(line_str.strip().split(',')) 
    return data_list 

當我運行get_data_list("player_regular_season.csv")我得到以下錯誤:

builtins.IOError: [Errno 2] No such file or directory:'player_regular_season.csv'

+0

嘗試指定完整的文件路徑(不要忘了逃跑斜線作爲''\\或使用'/'代替)。你也可能想使用['csv'](http://docs.python.org/library/csv.html)模塊。 – 2012-07-11 21:56:54

+1

您應該添加[家庭作業]標籤以保持公平。 – pepr 2012-07-12 06:36:35

回答

0

對於第一次嘗試,你可以把數據文件在同一目錄中Python程序並從目錄中啓動它。

也嘗試一個單一的目的腳本來學習如何使用目錄的工作。從標準文檔15.1.5. Files and Directories,即os.getcwd()os.chdir(path)學習的功能,然後10.1. os.path — Common pathname manipulations,即os.path.isfile(path)

但也看過其他功能的文檔中的文件,以瞭解什麼是可用的。

當知道如何與文件名和路徑的工作,看看在13.1. csv — CSV File Reading and Writing。不要害怕所有的東西,從最後開始 - 使用csv模塊的13.1.5. Examples