简体   繁体   中英

python csv file with several columns

Hello i want to create a csv file in python with the standard library csv. My Code:

csv_columns = ['receiptNr','category','date','allvalue', 'quantity', 'value']
dict_data = {'receiptNr': 293293, 'category': 'Sbudget' ,'date': '29.11.2020' ,'allvalue': '2.70' , 'quantity': '2 STK' , 'value': '1.35'}

csv_file = r"C:\Maturaprojekt\table.csv"


try:
    with open(csv_file, 'w') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=csv_columns, lineterminator = '\n')  
        writer.writeheader()       
        writer.writerow(dict_data)
        
except IOError:
    print("I/O error")

the output is: output

but i want to have: wantedfile

raw text data: data

That's problem of delimiter.

The delimiter used in your csv file is probably not the same delimiter used in your spreadsheet program.

You can add a delimiter parameter to csv.DictWriter to change the delimiter of your csv file if you want, like this:

writer = csv.DictWriter(csvfile, delimiter=';', fieldnames=csv_columns, lineterminator = '\n')

Or you could change the csv file delimiter chosen in your spreadsheet program.

As @Chris commented, you're showing us screenshots from a spreadsheet program, you will have to select the right delimiter in your spreadsheet program (usually when you open the csv file).

In short: the default delimiter used by csv.DictWriter is not the same that is used in your spreadsheet program.

How you could change the delimiter (separator) used by your spreadshet program depends on the spreadsheet program you use. You could go to your favorite search engine and search for something like this: csv file change separator libreoffice , or csv file change separator excel , or csv file change delimiter excel , etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM