【问题标题】:How can I store data to a data dictionary in Python when headings are in mixed up order当标题混淆时,如何将数据存储到 Python 中的数据字典中
【发布时间】:2011-09-09 08:44:15
【问题描述】:

我想将以下数据存储在数据字典中,以便轻松将其导出为 CSV 文件。问题是每个学校 id 的列的顺序并不总是相同:

text = """
school id= 28392
name|year|degree|age|race
Susan A. Smith|2007|PhD|27|white
Fred Collins|2006|PhD|26|hispanic
Amber Real|2007|MBA|28|white
Mike Lee|2003|PhD|27|white

school id= 273533123
name|year|age|race|degree
John B. Black|2003|27|hispanic|MBA
Steven Smith|2005|28|black|PhD
Jacob Waters|2003|25|hispanic|MBA

school id = 3452332
name|year|race|age|degree
Peter Hintze|2002|white|27|Bachelors
Ann Graden|2004|black|25|MBA
Bryan Stewart|2004|white|28|PhD
"""

我希望最终能够将所有数据输出到具有以下标题的 CSV 文件:

school id|year|name|age|race|degree

我可以在 Python 中做到这一点吗?

【问题讨论】:

  • 你有几所学校?它们很少,你可以手动输入吗?这会让问题变得更容易。
  • 不,有很多。这将需要大量手动输入,并且将来可能需要重复此过程。
  • 好的,但是,假设 id=28392 的学校总是有这个订单?姓名|年份|学位|年龄|种族

标签: python csv dictionary export-to-csv


【解决方案1】:

这实际上看起来很简单。将文件处理为数据结构,然后将其导出为 csv。

school = None
headers = None
data = {}
for line in text.splitlines():
    if line.startswith("school id"):
        school = line.split('=')[1].strip()
        headers = None
        continue
    if school is not None and headers is None:
        headers = line.split('|')
        continue

    if school is not None and headers is not None and line:
        if not school in data:
            data[school] = []
        datum = dict(zip(headers, line.split('|')))
        data[school].append(datum)    

In [29]: data
Out[29]: 
{'273533123': [{'age': '27',
                'degree': 'MBA',
                'name': 'John B. Black',
                'race': 'hispanic',
                'year': '2003'},
               {'age': '28',
                'degree': 'PhD',
                'name': 'Steven Smith',
                'race': 'black',
                'year': '2005'},
               {'age': '25',
                'degree': 'MBA',
                'name': 'Jacob Waters',
                'race': 'hispanic',
                'year': '2003'}],
 '28392': [{'age': '27',
            'degree': 'PhD',
            'name': 'Susan A. Smith',
            'race': 'white',
            'year': '2007'},
           {'age': '26',
            'degree': 'PhD',
            'name': 'Fred Collins',
            'race': 'hispanic',
            'year': '2006'},
           {'age': '28',
            'degree': 'MBA',
            'name': 'Amber Real',
            'race': 'white',
            'year': '2007'},
           {'age': '27',
            'degree': 'PhD',
            'name': 'Mike Lee',
            'race': 'white',
            'year': '2003'}],
 '3452332': [{'age': '27',
              'degree': 'Bachelors',
              'name': 'Peter Hintze',
              'race': 'white',
              'year': '2002'},
             {'age': '25',
              'degree': 'MBA',
              'name': 'Ann Graden',
              'race': 'black',
              'year': '2004'},
             {'age': '28',
              'degree': 'PhD',
              'name': 'Bryan Stewart',
              'race': 'white',
              'year': '2004'}]}    

【讨论】:

  • 是的,就是这样!打败我。
猜你喜欢
  • 2011-02-22
  • 2013-05-06
  • 2019-04-23
  • 1970-01-01
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多