-2

I want a CSV designed like this:

   A      B     C     D   
 ------ ----- ----- ----- 
  v1     v11   v12   v13  
  v2     v21   v22   v23  
  v3     v31   v32   v33  

I have a dictionary like this:

{"v1": ["v11", "v12", "v13"], "v2": ["v21", "v22", "v23"], "v3": ["v31", "v32", "v33"]}

I already tried this:

with open("cyclesAndSignalChange.csv", 'wb') as csvfile:
    wr = csv.DictWriter(csvfile, cycle_with_signal_change.keys(), delimiter = ' ')
    for i in cycle_with_signal_change:
        wr.writeheader()
        wr.writerow(cycle_with_signal_change)

But this just gives me a 1GB large file whereas my data is like 1MB.

How can I achieve this?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
MagikarpSama
  • 323
  • 1
  • 11
  • use pandas to manage the records and export it as a csv. – Pedro Lobito May 13 '18 at 13:41
  • Why 'vertically'? You have 3 sequences, write those as rows. – Martijn Pieters May 13 '18 at 13:41
  • What you're looking for is a `transpose`. – Tom Wojcik May 13 '18 at 13:45
  • ***"I have a dictionary like this"*** - No, you don't have valid python dictionary with `[ "v1": ("v11", "v12", "v13"), "v2": ("v21", "v22", "v23"), "v3": ("v31", "v32", "v33")]`. Change the square brackets `[]` to curly braces `{}` and you'll have a dictionary. – Pedro Lobito May 13 '18 at 13:47
  • Are you expecting to output the exact same format as in your post, with the same large amount of whitespace and the `-----` header - rows divider? If so, the `csv` module is the wrong tool anyway. – Martijn Pieters May 13 '18 at 13:51

2 Answers2

1

You are writing your whole dictionary as a row, for every key in the dictionary, so you are outputing an exponential amount of data; the fact you are adding a header for each row only upping the damage.

You need to treat each key-value pair as a row here:

with open("cyclesAndSignalChange.csv", 'wb') as csvfile:
    wr = csv.writer(csvfile, delimiter=' ')
    wr.writerow(['A', 'B', 'C', 'D'])
    for key, value in cycle_with_signal_change.items():
        wr.writerow([key, *value])

This outputs the key-value pairs in arbitrary order; if you need to have a specific ordering, sort first or use an ordered data structure.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

Why don't you use pandas for this task? i.e.:

import pandas as pd
test = {"v1": ("v11", "v12", "v13"), "v2": ("v21", "v22", "v23"), "v3": ("v31", "v32", "v33")}
df = pd.DataFrame.from_items(test.items(), orient='index',  columns=['B','C','D'])
df.index.name = 'A'
df.to_csv("test.csv", sep='\t', encoding='utf-8')

test.csv

A   B   C   D
v1  v11 v12 v13
v2  v21 v22 v23
v3  v31 v32 v33

PS: If you need the fancy ------ ----- ----- ----- just append it programmatically after the csv header, but remember you'll have to skip this line when you parse the csv file.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • 1
    Pandas is great if you need it for data analysis. It’s a pretty hefty thing to install just the write a CSV file, where the standard library can already cover this use case just fine. – Martijn Pieters May 13 '18 at 14:19
  • @MartijnPieters The last part of your current answer is the reason I used pandas. – Pedro Lobito May 13 '18 at 14:22
  • so `sorted()` is a reason to install Pandas? – Martijn Pieters May 13 '18 at 14:23
  • Hahah, no, just for the sake of simplicity The code above runs under 500ms and, in this case, I don't see the advantage of importing the `csv` module, assuming that pandas is already installed on the OP system and the OP isn't *obliged* to use the CSV module. – Pedro Lobito May 13 '18 at 14:35