Write list to csv file vertically

Viewed 223

 What could be the best way to use a list set and create or export this data to a csv file?

 To begin with, I must highlight that my data is of type string so I will need a method to collect these and filter the commas and values as integer type.

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})
3 Answers

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.

  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)
'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

Just use csv:

import csv

signals = ['-108', '-107', '-107', '-107', '-107', '-107', '-94', '-87', '-108']
costs = ['8325', '5175', '2698', '1754', '4767', '3652', '2417', '7527', '3698']

with open("Data_to_Csv.csv", "w") as f:
    writer = csv.writer(f)
    writer.writerow(["Signals", "Costs"])
    writer.writerows(zip(signals, costs))

But if you find convtools useful, then you could:

from convtools.contrib.tables import Table

Table.from_rows(
    zip(signals, costs),
    header=["Signals", "Costs"]
).into_csv("Data_to_Csv.csv")
Related