0

I extracted the following set of data and placed them into a dictionary. What is the best way to represent this data in python csv?

After the first 4 key-value pairs, I have 4 keys with nested lists as values in order to store the information contained in them.

The nested lists are problematic as they do not show up properly in csv. If I attempt to store each string in the nested list as an independent cell, it will also affect the comprehension of the first 4 key-value pairs.

Hoping someone can give advice on this dilemma.

{
'ID': '1', 
'Name': 'PROF DR BACHTIAR ALY MA', 
'Faction': 'National Democrat Party Faction', 
'Constituency': 'ACEH I', 
'Educational Background': ['Sekolah Rakyat Tahun:  - 1963', ' SMPN 1 Banda Aceh Tahun:  - 1965', 'IPS SMA YPU Bandung Tahun:  - 1968', 'Fakultas Publistik UNPAD Tahun: 1969 - 1971', 'Komunikasi Westfaelishe Wilhemlins Tahun: 1974 - ', 'Philosophische Universitas Westfalischen Jerman Tahun:  - 1984'],
'Working Experience': ['Pokja Pengaduan Dewan Pers Sebagai: Anggota Tahun: 2003 - 2006', 'Anggota Dewan Pers Sebagai: Anggota Tahun: 2000 - 2003', 'Penasehat Ahli Kapolri Sebagai: Penasehat Tahun: 1999 - 2014', 'Konsultan PT Arun Sebagai:  Tahun: 1989 - 1994', 'Kadin Indonesia Komite Cina Sebagai: Staf Ahli Tahun: 1985 - 1986', 'Penasehat Presiden Urusan Aceh Sebagai: Penasehat Tahun:  - '],
'Organizational': ['BAKOM PKB Sebagai: Ketua Tahun: 1990 - 1995', 'PPI & IASI Jerman Sebagai: Ketua Tahun: 1976 - 1982', 'Ketua Warga SMP & Ketua Osis SMA Sebagai:  Tahun: 1965 - 1968', 'KAPPI Daerah Itimewa Aceh Sebagai: Ketua Tahun:  - 1968'], 
'Movement': ['KAPPI Aceh - Ketua Tahun:  - 1968']
}
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 2
    Well CSV stores flat rows & columns, you cannot really represent asymmetric data like your JSON in that format, what do you imagine the CSV would look like? – Alex K. Jul 09 '19 at 13:28
  • 2
    Exactly that. This isn't really a question about Python, or about CSV; it's a question about design of your output format. A good place to start would be to figure out what a spreadsheet you made with this data *should* look like, perhaps even by making one by hand. As it is, though, this doesn't fit inside our required scope of being a narrow technical question; it's not a technical problem at all, it's a problem of needing to determine design intent. – Charles Duffy Jul 09 '19 at 13:34
  • 1
    Possible duplicate of [How can I convert JSON to CSV?](https://stackoverflow.com/questions/1871524/how-can-i-convert-json-to-csv) – sophros Jul 09 '19 at 13:34
  • Try this: Encapsulate the nested data into strings, and change the separating character from ',' to '&' or something else. Then you just write your data in a plain csv. – Julio P.C. Jul 09 '19 at 13:37
  • Let's take Educational Background for instance. I was thinking of creating a code that can check the number of items in it, and then creates Educational Background 1, Educational Background 2, Educational Background 3 horizontally as necessary. – Jefferson Ng Jul 09 '19 at 13:38
  • The comment from @sophros has a proper answer to your question. – Julio P.C. Jul 09 '19 at 13:39
  • Do you have only one data item, or do you need to create as many columns are necessary for the longest instance of any field? – Charles Duffy Jul 09 '19 at 13:40
  • I have 560 IDs to go, each represented from top to bottom. So the only space to populate the additional items in the nested lists is sideway. – Jefferson Ng Jul 09 '19 at 13:43
  • *"... horizontally as necessary"* the problem with this is then knowing where the next set of data ('Working Experience') begins, of course you could work out the maximum number of 'Educational Background' then pad all the other rows to that but it's still a poor solution. What is actually going to consume the CSV? – Alex K. Jul 09 '19 at 13:44

0 Answers0