Home > Net >  Write list to csv file vertically
Write list to csv file vertically

Time:12-02

I've been trying to write lists to a csv file. I've managed to remove the commas and the quotes from the lists, but I couldn't add the lists to the csv file vertically like I need to. Is there a way that I could solve it? My lists are like below:

signals = ['-108', '-107', '-107', '-107', '-107', '-107', '-94', '-87', '-108']

Costs = ['8325', '5175', '2698', '1754', '4767', '3652', '2417', '7527', '3698']

What I would like:

enter image description here

I'm getting the file like this: enter image description here

The part of my code that write to the csv file:

import csv

fieldnames = ['Signals','Costs']

s = ' '.join(signals)
c = ' '.join(costs)

with open('Data_to_Csv.csv', 'w') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerow({'Signals':s,'Costs':c})

CodePudding user response:

I am assuming that the length of signals and Costs is the same. If that is true, here is your problem:

s = ' '.join(signals)
c = ' '.join(costs)

What this does is created a string called s with the value being all the numbers in signals joined together with spaces and a string called c with the value being all the numbers in costs. So effectively you will have the following:

>>> signals = ['-108', '-107', '-107', '-107', '-107', '-107', '-94', '-87', '-108']
>>> costs = ['8325', '5175', '2698', '1754', '4767', '3652', '2417', '7527', '3698']
>>> s = ' '.join(signals)
>>> c = ' '.join(costs)
>>> print(s)
-108 -107 -107 -107 -107 -107 -94 -87 -108
>>> print(c)
8325 5175 2698 1754 4767 3652 2417 7527 3698

So when you call writer.writerow({'Signals': s, 'Costs': c}), it will just concatenate the two strings with a ',' between them. This is what you see in your resultant CSV file.

What you need to do is loop over the list and for each index in signals, get the cost and then write that into your CSV file. Something like this:

with open('Data_to_Csv.csv', 'w') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    for i in range(len(signals)):
        writer.writerow({'Signals':signals[i],'Costs':costs[i]})

This will result in a Data_to_Csv.csv file like this:

Signals,Costs
-108,8325
-107,5175
-107,2698
-107,1754
-107,4767
-107,3652
-94,2417
-87,7527
-108,3698

Note that this approach will work only if len(signals) == len(costs). If that is not true, you need to handle that case.

CodePudding user response:

  use pandas.DataFrame.to_csv from pandas and export your current data in a csv file.

>>> import pandas 
>>> signals = ['-108', '-107', '-107', '-107', '-107', '-107', '-94', '-87', '-108']
>>> costs = ['8325', '5175', '2698', '1754', '4767', '3652', '2417', '7527', '3698']
>>> df = pandas.DataFrame({'Signals':signals, 'Costs':costs})
>>> df.to_csv(index=False, compression=compression_opts)
'Signals,Costs\r\n-108,8325\r\n-107,5175\r\n-107,2698\r\n-107,1754\r\n-107,4767\r\n-107,3652\r\n-94,2417\r\n-87,7527\r\n-108,3698\r\n'

compress and write a csv file:

>>> compression_opts = dict(method='zip', archive_name='out.csv')
>>> df.to_csv(index=False, compression=compression_opts)

csv file

  • Related