0

I have a list called itemresults that looks like this:

['24500', '17000', '60000', '56300', '24500', '24500', '114950', '32000', '70000', '46948', '133000', '49550', '51600', '35995', '44500', '19300', '34200', '39964', '59900', '27999', '16100', '39000', '78995', '19101', '25000', '30000', '72000', '39900', '30000', '29000', '24900', '22990', '52600', '82600', '64000', '13999', '29500', '46800', '95000', '42900', '32500', '3969', '36000', '32995', '24000', '49999.99', '37500', '34000', '47600', '43500', '28500']

I would like to write this to a csv file so that each row, would be an item from the list so essentially looking like this:

24500
17000
60000
56300
24500
.....

I have tried doing this with different methods, but each time, it just writes the whole list in one cell. I have tried using a nested list like this:

outputFile = open('outputFile.csv', 'w', newline='') 
outputWriter = csv.writer(outputFile)
outputWriter.writerow([elements])

But that still just writes the whole list to the first cell. I am looking to write each item to a new row. Does anyone know how I could achieve this? Thanks

cordmana
  • 121
  • 3
  • 12

4 Answers4

2

Try This Out..

import csv

itemresults = ['24500', '17000', '60000', '56300', '24500', '24500', '114950', '32000', '70000', '46948', '133000', '49550', '51600', '35995', '44500', '19300', '34200', '39964', '59900', '27999', '16100', '39000', '78995', '19101', '25000', '30000', '72000', '39900', '30000', '29000', '24900', '22990', '52600', '82600', '64000', '13999', '29500', '46800', '95000', '42900', '32500', '3969', '36000', '32995', '24000', '49999.99', '37500', '34000', '47600', '43500', '28500']


filename = 'outputFile.csv'
with open(filename, 'w') as csvfile:  
    csvwriter = csv.writer(csvfile)
    for x in itemresults:
        csvwriter.writerow([x])
RuFerdZ
  • 46
  • 5
0

There is no need to use the csv.writer here. You can just write the numbers directly and format them using '\n'.join(itemresults):

...
outputFile.write('\n'.join(itemresults))
...
Matt
  • 1,148
  • 10
  • 15
0

You can use regular python file handling to achieve this like so:

your_list = ['24500', '17000', '60000', '56300', '24500', '24500', '114950', '32000', '70000', '46948', '133000', '49550', '51600', '35995', '44500', '19300', '34200', '39964', '59900', '27999', '16100', '39000', '78995', '19101', '25000', '30000', '72000', '39900', '30000', '29000', '24900', '22990', '52600', '82600', '64000', '13999', '29500', '46800', '95000', '42900', '32500', '3969', '36000', '32995', '24000', '49999.99', '37500', '34000', '47600', '43500', '28500']

with open("outputFile.csv", "w") as outputFile:
  for item in your_list:
    outputFile.write(f'{item}\n')

Shreyas Sreenivas
  • 331
  • 1
  • 5
  • 12
0

create a DataFrame from the list and then create the csv

# import pandas as pd 
import pandas as pd 
  
# list of strings 
lst = ['Geeks', 'For', 'Geeks', 'is',  
            'portal', 'for', 'Geeks'] 
  
# Calling DataFrame constructor on list 
df = pd.DataFrame(lst) 

df.to_csv("result.csv")
Dean Taler
  • 737
  • 1
  • 10
  • 25