1

I have Python dict containing 4 key value pairs. Each value is a numpy arrays. Now I want to print the whole dict to a csv, forcing to write one numpy array per row.

with open(os.path.join("csv", title), 'w', newline='') as f:
  w = csv.DictWriter(f, list(data.keys()))
  w.writeheader()
  w.writerow(data)

Is what I have used yet. But some of my arrays get written to several rows instead of a single line.

Here an example of input data:

{'DE': array([[ 38574. , 38538.1904, 39511.6190, 42521.1428, 50586. , 46282.5238, 42714.4761, 40612.0476], [ 42798.4666, 42112.5333, 42277.8666, 42886.1333, 50224.3333, 48148.8 , 44272.6666, 41210.2 ]])}

I expect the output so that, each line of my array is written on one line. Instead I get a file containing "\n" after a certain amount of digits. how can i force to write the whole array in one row?

4
  • 2
    Could you provide a minimal example of input data? Commented Aug 9, 2014 at 14:01
  • 1
    So what do you expect the output to look like? Commented Aug 9, 2014 at 14:09
  • sorry for that, i edited and hopefully this should answer the questions Commented Aug 9, 2014 at 14:33
  • your first example is not correct, Dictwriter writes each column based on the keys, the output should be in separate columns not one below the other Commented Aug 9, 2014 at 14:43

1 Answer 1

1

DE has a multidimensional array as its value, Inter has an empty list as its value, you end up with two columns one with Inter as the header with an empty list in its column and a second column DE with the array in its column which is exactly what the code should be doing.

If you want to alter each array length try setting numpy.set_printoptions:

numpy.set_printoptions(linewidth=1000)
Sign up to request clarification or add additional context in comments.

2 Comments

Yes and no. I end up with the following file opened in excel: A1: DE, INTER, A2: 38574, 38538, 39511, 42521, A3: 48751, 54737, 58725, 60445, A4: 50586, 46282, 42714, 40612] and if i open it with the editor it shows me the "\n" after 4 values. and i don't know where they come from.
actually it is still not working as intended. Sometimes when i open the csv in excel everything is written to one cell and sometimes it is written as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.