#πŸ”’ Need help using the csv module

112 messages Β· Page 1 of 1 (latest)

green echo
#

I have some basic stuff i wanted to write in a csv file, I can't seem to find any solution online so any help will be appreciated. Issue is when I try to write it doesn't write in seperate lines but rather all in one, I want it to write in the different columns of the csv file. Here's the code:

def save_to_csv(self, filename="accounts.csv"):
        """Save all accounts to a CSV file."""
        with open(filename, mode="w", newline="", encoding="utf-8") as file:
            writer = csv.writer(file)
            writer.writerow(("Account ID", "Department", "Balance"))
            for account in self.accounts:
                writer.writerow((account.account_id, account.department_name,
                                 account.balance))```
potent pivotBOT
#

@green echo

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

grim briar
#

This looks ok. Can you copy/paste the contents if the CSV?

#

BTW, you shouldn't need the newline="", in the open() call.

stable ore
#

if you mean that it writes every line in a single cell, that isn't an issue with your code but with the software you're using, which doesn't automatically split your columns

grim briar
#

Hmm. I assume you "imported" the CSV. Did you tell it to use commas to separate columns?

stable ore
#

your code looks okay and the screenshot you shared rightly shows 3 lines

grim briar
#

@green echo ^^

stable ore
#

(its also specified in the official python doc for the csv module)

#

the csv module handles the newlines itself

green echo
green echo
grim briar
#

It looks like all the fields from your file went into a single column in the spreadsheet.

stable ore
#

depends, its excel right ?

green echo
stable ore
grim briar
#

Was there some kind of "import" dialogue, asking how to import the CSV file?

undone dune
#

i think you need openpyxel e.g. to write in excel

grim briar
stable ore
#

also the default delimiter of python's CSV module is a space

#

so you'd need to separate by spaces in the column separator tool

#

you can specify the delimiter as an argument of csv.writer

green echo
stable ore
#

its not, and you can see it in the screenshot you shared, all the values of a single row are delimited by a space

grim briar
stable ore
#

nvm it should be a colon πŸ€” but it's depending on the dialect used

grim briar
#

Test script:

import csv

with open("accounts.csv","w") as file:
    writer=csv.writer(file)
    writer.writerow(("Account ID", "Department", "Balance"))
    writer.writerow((1,"Football",900.0))
    writer.writerow((2,"Chess",700.0))
stable ore
#

its weird they dissapear on excel then

grim briar
#

Output:

[~/tmp/python-discord]fleet2*> cat accounts.csv
Account ID,Department,Balance
1,Football,900.0
2,Chess,700.0
grim briar
#

This mostly has to do with how strings are written into the file.

grim briar
stable ore
#

comma separated values ye

green echo
#

Ok so what am i supposed to try again

grim briar
#

@green echo can you copy/paste the text of your accounts.csv file here, as text please?

green echo
#

sure

#

Uh

green echo
grim briar
#

Open the file in a text editor.

#

Select all the text, type ^C (control C) (or cmd-C on a Mac).

green echo
#

Account ID,Department,Balance
1,Football,900.0
2,Chess,700.0

grim briar
#

Aye, like that.

#

Looks fine to me.

#

The file is called accounts.csv, yes?

green echo
#

Yup

#

Did you see the image i attached?

grim briar
#

(Just wondering if excel made some assumptions.)

grim briar
#

How's it look it you drag the column separators to make the excel columsn visually wider.

green echo
#

this is what happens

grim briar
#

So, to load this up in excel, exactly what did you do? (I haven't got excel here.)

green echo
grim briar
#

I thought you wrote the file using Python?

green echo
grim briar
#

Then why the Python code up the top?

green echo
#

Wait, the code is to write in the file and the file itself i just made on excel

green echo
grim briar
#

Right. That's a small part of a larger programme I assume.

grim briar
#

So, the first screenshot, with all the fields in the first column. Exactly how did you make the file and load it into excel?

#

The Python code you supplied looked basicly ok, and we assumed you'd used that code to write the file, and the screenshot was from the CSV file written by the python code.

#

My example script above was based on your code, and you can see it wrote a correct looking CSV file.

green echo
grim briar
#

So you opened an excel file. Made a spreadsheet with those columns and values. Exports as CSV from Excel?

#

You say "office". You don't mean MS Word do you?

green echo
grim briar
#

But. Still a word processor or something?

grim briar
#

So when you made it, it had 3 correct columns in it?

green echo
grim briar
#

If you made an excel file with, say, 1 column, and those columns had comas in there, excel would escape them in the CSV.

#

Example:

[~/tmp/python-discord]fleet2*> cat accounts.csv
Account ID,Department,Balance
1,Football,900.0
2,Chess,700.0
3,"One,Two,Three",700.0

That last line still has only 3 columns.

green echo
green echo
grim briar
#

Yes.

green echo
#

Its not

grim briar
#

Can you copy/paste the text from your CSV file here?

green echo
#

Sure

#

Account ID,Department,Balance
1,Football,900.0
2,Chess,700.0

#

Same as before

grim briar
#

And it's definitely a plain text file? I guess if Python wrote it it will be.

green echo
grim briar
#

Does Excel have some kind of "import" menu option? It should do the right thing given a CSV file, but I'm beginning to wonder. And I can't test here, no Excel.

green echo
#

Ill try importing it

grim briar
#

Ok. Is it asks for a separator, make sure to specify a comma.

green echo
#

still the same issue

grim briar
#

😦

#

I don't know what's going on then.

#

To my eye your CSV file looks correct.

green echo
#

I have no clue either

grim briar
#

Ask again at another time? Maybe someone else not presently here will have some insight.

green echo
#

So theres no issue with the code?

grim briar
#

The code seems fine to me.

green echo
grim briar
#

Yeah. I think the CSV is fine and something weird is happening with Excel.

green echo
#

Is there any other way to open csvs?

#

Like with rows and columns i mean

grim briar
#

A different speadsheet? In a text file?

#

Maybe fetch LibreOffice and try that?

green echo
#

I guess this is more of an excel issue than a python issue at this point

grim briar
#

I looks that way to me. SOrry.

green echo
#

πŸ‘ How can i mark this as done ty for the help btw

grim briar
#

Maybe just say !close.

green echo
#

!close

potent pivotBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.