3

Just having a bit of a struggle getting the right formatting with the csv output files.

I have the following list called found in python:

   [['dropbearid', 'distance'],
    ['DB_1487', 17.543651156695343],
    ['DB_1901', 24.735333924441772],
    ['DB_2800', 6.607094868078008]]

When I use

import csv
out = csv.writer(open("myfile.csv","w"), delimiter=',',quoting=csv.QUOTE_ALL)
out.writerow(found)

I get a file that when I open in excel has the values but it has 'dropbearid','distance' in one cell and 'DB_1487','17.54...' in the next cell and so on all continued across the first row.

Is there a way to get the output setup so 'dropbearid' and 'distance' are placed across two columns and all the list below are put in rows below?

Thanks!

Andy
  • 163
  • 1
  • 10
  • For inserting each row you need to call `out.writerow` seperately, or you may use `out.writerows()` – ZdaR Aug 31 '18 at 05:05
  • Pretty easy way to do that using numpy here https://stackoverflow.com/questions/6081008/dump-a-numpy-array-into-a-csv-file – A. J. Alger Aug 31 '18 at 05:06

6 Answers6

4

Welcome to Stackoverflow. You write the list of lists to one row, which is why you have two values in one cell, because you write every element of found (which is a list of two elements) in a cell. You need to iterate over the list of lists and write every list to a row. This should work:

import csv
out = csv.writer(open("myfile.csv","w"), delimiter=',',quoting=csv.QUOTE_ALL, newline='')
for row in found:
    out.writerow(row)
Bernhard
  • 1,227
  • 8
  • 18
1

The function writerow() will write a single row. So you need to use writerows() and set newline parameter as '' to avoid blank rows in file.

found = [['dropbearid', 'distance'],
    ['DB_1487', 17.543651156695343],
    ['DB_1901', 24.735333924441772],
    ['DB_2800', 6.607094868078008]]
import csv
with open('myfile.csv', 'w', newline='') as outfile:
    writer = csv.writer(outfile)
    writer.writerows(found)

Hope this helps! Cheers!

SanthoshSolomon
  • 1,296
  • 1
  • 10
  • 23
1
import pandas as pd
found = [['dropbearid', 'distance'],['DB_1487', 17.543651156695343],['DB_1901', 24.735333924441772],['DB_2800', 6.607094868078008]]
dffound = pd.DataFrame(found)
header = dffound.iloc[0]
dffound = dffound[1:]
dffound.rename(columns = header)
dffound.tocsv("enter path here")
vishalk
  • 152
  • 1
  • 10
0

Use pandas DataFrames for writing lists to CSV's, it makes formatting lot easier

import pandas as pd
dffound = pd.DataFrame(found,columns=['dropbearid', 'distance'])
dffound.to_csv('Found.csv')
Soubhik Banerjee
  • 342
  • 2
  • 6
  • 17
0

You would use the writerows function instead of writerow, to write each item of the sub-list as a column.

with open('myfile.csv', 'w+') as csv_file:
    writer = csv.writer(csv_file, delimiter=',')
    writer.writerows(d)
Akay Nirala
  • 1,086
  • 7
  • 12
0

Use pandas library for this.

import pandas as pd
df = pd.DataFrame(found, columns=['dropbearid', 'distance'])
df = df.drop(0) # drop the header row
df.to_csv('Found.csv', index=False)
Paresh
  • 616
  • 5
  • 7