python 把檔案中每列的第一個值作為字典中的key鍵值索引
阿新 • • 發佈:2019-02-03
所讀取的檔案內容:
20160309,2839.41,2863.01,2811.72,2862.56,183355383,177072553984.00
20160310,2847.57,2863.18,2803.48,2804.73,138979461,141812416512.00
20160311,2781.60,2815.61,2772.55,2810.31,127247554,128450314240.00
[email protected] stockInfo]# py ps.py
['20160310', '2847.57', '2863.18', '2803.48', '2804.73', '138979461', '141812416512.00']
- import codecs
- symbol = '999999'
- csvFile = '/home/www/stock/stockInfo/stockInfo_data_index/'+str(symbol)
- File = codecs.open(csvFile,'r','gb18030') #讀取檔案
- line_count = 0
- item = {}
- for line in File: #迴圈讀取每行
- line_list = line.strip() #移除字串(一行)頭尾的空格
- item_count = 0
- for r in line_list.split(','): #把該行以逗號','分割後分別放進變數r
- if item_count == 0 : #每行第一列的日期放進變數date
- date = int(r)
- item_count +=1
- item[date] = line_list.split(',') #把當前整列的內容放進所對應日期(key)的值(value)的字典變數item中
- line_count +=1
- print(item[20160310])