1

I have successfully exported the data I receive from the rows I read into a csv file, but I need to set a Header for each row so I can identify them. I have tried this:

file = open("llistaClients.csv", "w")
file.write("ID;NAME;PHONE;EMAIL;STREET;CITY")
for row in rows:        
    print(row['id'], row['name'], row['phone'], row['email'], row['street'], row['city'], sep="\t")
    dades = str(row['id']) + ";" + str(row['name']) + ";" + str(row['phone']) + ";" + str(row['email']) \
            + ";" + str(row['street']) + ";" + str(row['city']);
    file.write(dades + "\n")

Without this file.write("ID;NAME;PHONE;EMAIL;STREET;CITY") it all works perfectly:

enter image description here

But if I try to use it, it just won't open with Excel. But, if I open the same file with the notepad it indeed applies this header.

I have a potential fix which lets me put the header without problem using: (other fields)

    import csv

    with open("llistaProductes.csv","w") as csvfile:

    fieldnames = ['id','name','list_price','create_date'];
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader();
    for row in rows:
        #print(dict(row))
        print(row['id'], row['name'], row['list_price'], row['create_date'], sep="\t")
        writer.writerow({'id': row['id'], 'name': row['name'],
                         'list_price': row['list_price'], 'create_date': row['create_date']})

But using this I get this in excel:

enter image description here

It doesn't separate between columns.. Which is not bad, but not how it should do..

So the question here is, why can't I use the first method properly? And if the second is the one I should use how to separate the columns. Thank you.

2
  • Seems like you forgot the newline after your header: file.write("ID;NAME;PHONE;EMAIL;STREET;CITY\n") Commented Jan 16, 2019 at 20:15
  • Oh, I did try with it but didn't put it in the question. Not working with it either.. :/ Commented Jan 16, 2019 at 20:36

1 Answer 1

1

There has to be a newline after the header:

file.write("ID;NAME;PHONE;EMAIL;STREET;CITY\n")

I see no reason why Excel wouldn't open this file. Do you get an error message?

As for the second example, you can tell Excel to use a certain separator by adding the following in the beginning of the file:

sep=,
id,name,email, ..
Sign up to request clarification or add additional context in comments.

4 Comments

Excel says the format and the extension of the file are not the same. It gives me the chance to discard and open, I do so and it gives me registry errors ( example: can't read registry 70). And then if I discard that, it won't let me open the file.
And the sep=, works with the command print, it's a utility print has, nothing to do with the csv stuff.
@Raül But the same file (with ; sep) opens fine without the header? Strange.
@Raül If Excel sees a sep=, (or sep=;) as the first line of the csv file it will (should) use that as the separator (though tehnically this is not 'standard' csv). Not sure what you mean with command/utility print.

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.