[英]Reading in CSV file to add data to a dictionary or tuple
我是Python的新手。 我需要重寫我的選股程序,並且需要入門幫助。
第一個模塊是主數據創建程序。 它讀取CSV EOD(一天結束)庫存數據文件。
然后,它解析一個主文件,將當日的庫存信息添加到一個數組(包含35個元素)中。
每個陣列都有5列,其中包含EOD信息。
原始程序以股票名稱編制索引。 索引並不重要,因為主文件只能在第二個主庫存程序中順序訪問。 需要的是將股票名稱用作主文件中(排列的)記錄的標題。
需要一種打開和讀取CSV文件的方法。 比較每只股票的名稱if stka == stkb
更新的信息main.idx
。
Open-r CVS
Open-rwb main
While (cvs)
Readline(CVS)
Readline (main)
If cvs == main
Add to record
Wright (main)
else
If cvs > main
Readline (main)
else
If cvs < main
Readline (cvs)
這是一些處理CSV程序和我的嘗試的代碼,位於自動更新CSV文件下。
我的需求更簡單,因為我只需要讀取CSV文件即可。 主文件可以位於任何Python數據集list
, dictionary
或tuple
。
感謝您為我指明正確方向的幫助。
您不太清楚所使用的文件格式,或者在讀入記錄后需要對它們做些什么。但是從閱讀CSV文件的另一個問題中獲取一些文件格式,下面我舉一個例子從工作程序。
這是一個主文件m.csv.txt
。 這是一個逗號分隔值(CSV)文件,第一個字段是標識符。
AAC,D,20111207,9.83,9.83,9.83,9.83,100
AACC,D,20111207,3.46,3.47,3.4,3.4,13400
AACOW,D,20111207,0.3,0.3,0.3,0.3,500
AAME,D,20111207,1.99,1.99,1.95,1.99,8600
AAON,D,20111207,21.62,21.9,21.32,21.49,93200
AAPL,D,20111207,389.93,390.94,386.76,389.09,10892800
這是每日更新文件d.csv.txt
。 它具有相同的格式。 同樣,第一個字段是標識符。 如果標識符與主文件中的標識符相同,則應合並條目。 否則,每日文件中的新標識符將添加到總體列表中。
AACC,D,20120127,4.01,4.02,4.03,4.04,40000
B,D,20120127,4.01,4.02,4.03,4.04,40000
這是一個簡單的程序,可以使用csv
模塊讀取主文件,並將每行的條目放入字典stocks
。 第一個字段用作字典的鍵。 這是一個非常簡單的數據結構,但是您說主文件可以是任何數據格式。 然后我們打印出stocks
字典。
import csv
mainReader = csv.DictReader( open("m.csv.txt","rb"), ["id"],"others")
newReader = csv.DictReader( open("d.csv.txt","rb"), ["id"],"others")
stocks = {}
for line in mainReader:
stocks[line['id']] = line
print stocks # output hand-formatted
{
'AAC': {'id':'AAC',
'others': ['D','20111207','9.83','9.83','9.83','9.83','100']},
'AACC': {'id':'AACC',
'others': ['D', '20111207', '3.46', '3.47', '3.4', '3.4', '13400']},
'AAME': {'id': 'AAME',
'others': ['D', '20111207', '1.99', '1.99', '1.95', '1.99', '8600']},
'AACOW': {'id': 'AACOW',
'others': ['D', '20111207', '0.3', '0.3', '0.3', '0.3', '500']},
'AAPL': {'id': 'AAPL', 'others':
['D','20111207','389.93','390.94','386.76','389.09','10892800']},
'AAON': {'id': 'AAON',
'others': ['D', '20111207', '21.62', '21.9', '21.32', '21.49', '93200']}
}
現在,該程序繼續使用csv模塊讀取每日更新文件。 如果一個條目具有相同的標識符,則不清楚該條目應如何與主文件中的條目組合。 對於此示例,我只讓每日文件中的條目完全替換現有條目。 他們我們打印出產生的stocks
。
for line in newReader:
# can use if line['id'] in stocks: to check for an existing record if desired
# in this example, we blindly overwrite any existing entry
stocks[line['id']] = line
print stocks # output hand-formatted
{
'AAME': {'id': 'AAME',
'others': ['D', '20111207', '1.99', '1.99', '1.95', '1.99', '8600']},
'B': {'id': 'B',
'others': ['D', '20120127', '4.01', '4.02', '4.03', '4.04', '40000']},
'AACC': {'id': 'AACC',
'others': ['D', '20120127', '4.01', '4.02', '4.03', '4.04', '40000']},
'AAPL': {'id': 'AAPL',
'others': ['D','20111207','389.93','390.94','386.76','389.09','10892800']},
'AAON': {'id': 'AAON',
'others': ['D', '20111207', '21.62', '21.9', '21.32', '21.49', '93200']},
'AACOW': {'id': 'AACOW',
'others': ['D', '20111207', '0.3', '0.3', '0.3', '0.3', '500']}
}
請注意,字典數據結構在打印時沒有任何特定順序。 您想要排序的輸出? 對字典使用items()
方法獲取列表,並使用.sort()
對列表進行排序。 (還有許多其他方法可以執行此操作。)
s = stocks.items()
s.sort()
print s # output hand-formatted
[
'AACC': {'id': 'AACC',
'others': ['D', '20120127', '4.01', '4.02', '4.03', '4.04', '40000']},
'AACOW': {'id': 'AACOW',
'others': ['D', '20111207', '0.3', '0.3', '0.3', '0.3', '500']},
'AAME': {'id': 'AAME',
'others': ['D', '20111207', '1.99', '1.99', '1.95', '1.99', '8600']},
'AAON': {'id': 'AAON',
'others': ['D', '20111207', '21.62', '21.9', '21.32', '21.49', '93200']},
'AAPL': {'id': 'AAPL',
'others': ['D','20111207','389.93','390.94','386.76','389.09','10892800']},
'B': {'id': 'B',
'others': ['D', '20120127', '4.01', '4.02', '4.03', '4.04', '40000']}
]
您是否要寫出更新的主文件。 使用csv.DictWriter()
或csv.writer()
。 您可以逐行執行此操作,也可以一次輸出完整的內存清單,例如全部stocks
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.