【问题标题】:Reading in CSV file to add data to a dictionary or tuple读取 CSV 文件以将数据添加到字典或元组
【发布时间】:2012-01-22 01:45:00
【问题描述】:

我是 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 程序的代码和我的尝试,在 Updating CSV files automatically 下找到。

我的需求更简单,因为我只需要读取 CSV 文件。主文件可以在任何 Python 数据集 listdictionarytuple 中。

感谢您为我指明正确方向的任何帮助。

【问题讨论】:

  • 从你发布的代码中不是特别清楚你在追求什么,但你应该看看 Python 的 csv 模块。
  • 您尝试做的事情应该很简单,但从您的描述中并不完全清楚。如果您有一些示例数据可能会有所帮助。
  • 我有点强迫症和吹毛求疵,但它的拼写是tuple,而不是tupol

标签: python arrays file


【解决方案1】:

你不是很清楚你正在使用的文件格式,或者你在读入记录后需要做什么。但是从your other question on reading CSV files获取一些文件格式,我会给你一个示例程序工作。

这是一个主文件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 module读入主文件的简单程序,并将每一行的条目放入字典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

【讨论】:

    猜你喜欢
    • 2015-06-24
    • 1970-01-01
    • 2023-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多