0

I have a program that creates two very large lists of float numbers.

We can call them list_x and list_y. They look like this:

list_x = [0.555, 0.5552, 0.55553]
list_y = [0.152, 0.155, 0.160]

I want it two be stored into a csv like this:

Column 1  Column 2
0.555      0.152
0.5552     0.155
0.55553    0.160

Here is my code:

with open('data.csv','w', newline = '') as out:
        csv.writer(out, delimiter=' ', quoting=csv.QUOTE_MINIMAL).writerows(zip(list_x, list_y))

However, it prints it like this instead:

Column 1
0.555   0.152
0.5552  0.155
0.55553 0.160

And no column 2.

Thanks.

2 Answers 2

1

When I try your code, I do not get any header line.

One solution would be to explicitly add headers to the list of items to be written, e.g.,

headers = [("Column 1", "Column 2")]
with open('data.csv','w', newline = '') as out:
    csv.writer(out, delimiter=' ', quoting=csv.QUOTE_MINIMAL).writerows(headers + zip(list_x, list_y))
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry there are no headers in the file I just wanted to show that I wanted them in 2 columns
0

Because you are using delimiter as ' ' [delimiter=' ']. so it splits 'Column 1' itself as 'Column' and '1', two separate headers.

2 Comments

Or, change the delimiter to \t
Thanks, I just removed the delimiter and it made 2 columns, thanks! \t does not work by the way

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.