I'm trying to write a code that gets a list of strings, and writes a csv file that containes all of these strings in one line.
here's what I got so far-
import csv
l = ['column1', 'column2',....]
with open('csvfile', 'w', newline='') as f:
writer = csv.writer(f, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for x in range(1):
writer.writerow(l)
I want the file to look like this:
column1 column2,...
but for some reason, it ends up looking like this:
column1 column2,...<\n>
I'm using windows and python 3.6.7.
Thanks in advance!
You dont need to use csv.writter you can just do it with Python's file handlers. Also dont use list as a variable name as its a keyword in Python.
list_columns = ['column1', 'column2', 'column3']
f = open('csvfile.csv', 'w')
for item in list_columns:
f.write(item)
f.write(' ')
f.close()
Output: column1 column2 column3
If you want to use csv.writer, you will need to write to a string buffer and then do some editing:
import csv
from io import StringIO
l = ['column1', 'column2']
out = StringIO('')
writer = csv.writer(out, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for x in range(4):
writer.writerow(l)
# replace newlines with single space and get rid of carriage returns:
rows = out.getvalue().replace('\n', ' ').replace('\r', '')
# final character is a space. Don't output:
with open('csvfile', 'w') as f:
f.write(rows[0:-1])
Related
I have a sentence like !=This is great
How do I write the sentence into a .CSV file with "!" in the first column and "This is great" in another column?
You can use pandas to_csv method
code:
import pandas as pd
col1 = []
col2 = []
f = '!=This is great'
l1 = f.split('=')
col1.append(l1[0])
col2.append(l1[1])
df = pd.DataFrame()
df['col1'] = col1
df['col2'] = col2
df.to_csv('test.csv')
split the text, and write it to an output file:
text = open('in.txt').read() #if from input file
text = '!=This is great' #if not from input file
with open('out.csv','w') as f:
f.write(','.join(text.split('=')))
output:
!,This is great
if you have multiple lines, you will have to loop through the input file and split each one
Of course, you could write using standard io with open() and manually write with comma delimiter for each line, but python has csv standard library that will help you with this. You could specify the dialect
In [1]: import csv
In [2]: sentence="!=This is great"
In [3]: with open("test.csv", "w", newline='') as f:
...: my_csvwriter = csv.writer(f)
...: my_csvwriter.writerow(sentence.split("="))
With multiple data, assuming it's in list, you could iterate through it when writing.
with open("test.csv", "w", newline='') as f:
my_csvwriter = csv.writer(f)
for sentence in sentences:
my_csvwriter.writerow(sentence.split("="))
This library helps handling comma in a sentence, instead of handling it yourself. For instance you have:
sentence = "!=Hello, my name is.."
with open("test.csv", "w", newline='') as f:
my_csvwriter = csv.writer(f)
my_csvwriter.writerow(sentence.split("="))
# This will be written: !,"Hello, my name is.."
# With that quote, you could still open it in excel without confusing it
# and it knows that `Hello, my name is..` is in the same column
I'm trying to combine two lists into a csv, and have it output a line per each line of a second list.
a.csv
1
2
3
b.csv
a,x
b,y
c,z
Output:
c.csv
1|a|x
2|a|x
3|a|x
1|b|y
2|b|y
3|b|y
1|c|z
2|c|z
3|c|z
So for each line of "a" combine each line of "b", and get a list in "c".
Note, I have no need to separate "b" to reorder the columns, keeping the original order is fine.
A loop seems needed, but I'm having zero luck doing it.
Answered (output is not perfect, but ok for what i was needing):
import csv
from itertools import product
def main():
with open('a.csv', 'rb') as f1, open('b.csv', 'rb') as f2:
reader1 = csv.reader(f1, dialect=csv.excel_tab)
reader2 = csv.reader(f2, dialect=csv.excel_tab)
with open('output.csv', 'wb') as output:
writer = csv.writer(output, delimiter='|', dialect=csv.excel_tab)
writer.writerows(row1 + row2 for row1, row2 in product(reader1, reader2))
if __name__ == "__main__":
main()
Output file:
1|a,x
1|b,y
1|c,z
2|a,x
2|b,y
2|c,z
3|a,x
3|b,y
3|c,z
Yes the "|" is only one of the separators.
It would be nice to know how to get "1|a|x" and so on.
One way is to use pandas:
import pandas as pd
df = pd.concat([pd.read_csv(f, header=None) for f in ('a.csv', 'b.csv')], axis=1)
df.to_csv('out.csv', sep='|', index=False, header=False)
A native Python approach, using itertools.product:
from itertools import product
#read file a, remove newline, replace commas with new delimiter and ignore empty lines
a = [line[:-2].strip().replace(",", "|") for line in open("a.csv", "r") if line[:-2].strip()]
#read file b, leave newline in string
b = [line.replace(",", "|") for line in open("b.csv", "r") if line[:-2].strip()]
#combine the two lists
c = ["|".join([i, j]) for i, j in product(a, b)]
#write into a new file
with open("c.csv", "w") as f:
for item in c:
f.write(item)
#output
1|a|x
1|b|y
1|c|z
2|a|x
2|b|y
2|c|z
3|a|x
3|b|y
3|c|z
I am trying to add a column to a csv file that combines strings from two other columns. Whenever I try this I either get an output csv with only the new column or an output with all of the original data and not the new column.
This is what I have so far:
with open(filename) as csvin:
readfile = csv.reader(csvin, delimiter=',')
with open(output, 'w') as csvout:
writefile = csv.writer(csvout, delimiter=',', lineterminator='\n')
for row in readfile:
result = [str(row[10]) + ' ' + str(row[11])]
writefile.writerow(result)
Any help would be appreciated.
No input to test, but try this. Your current approach doesn't include the existing data for each row that already exists in your input data. extend will take the list that represents each row and then add another item to that list... equivalent to adding a column.
import csv
with open(filename) as csvin:
readfile = csv.reader(csvin, delimiter=',')
with open(output, 'w') as csvout:
writefile = csv.writer(csvout, delimiter=',', lineterminator='\n')
for row in readfile:
row.extend([str(row[10]) + ' ' + str(row[11])])
writefile.writerow(row)
I assume that glayne wants to combine column 10 and 11 into one.
In my approach, I concentrate on how to transform a single row first:
def transform_row(input_row):
output_row = input_row[:]
output_row[10:12] = [' '.join(output_row[10:12])]
return output_row
Once tested to make sure that it works, I can move on to replace all rows:
with open('data.csv') as inf, open('out.csv', 'wb') as outf:
reader = csv.reader(inf)
writer = csv.writer(outf)
writer.writerows(transform_row(row) for row in reader)
Note that I use the writerows() method to write multiple rows in one statement.
Below code snippet combines strings in column 10 and column 11 in each row and add that to the end of the each row
import csv
input = 'test.csv'
output= 'output.csv'
with open(input, 'rb') as csvin:
readfile = csv.reader(csvin, delimiter=',')
with open(output, 'wb') as csvout:
writefile = csv.writer(csvout, delimiter=',', lineterminator='\n')
for row in readfile:
result = row + [row[10]+row[11]]
writefile.writerow(result)
I have a list of pathnames:
li = [u"C:\\temp\\fileA.shp", u"C:\\temp\\fileB.shp", u"C:\\temp\\fileC.shp"]
I am trying to write each path on a separate line in a txt file. This is what I have done so far:
import csv
li = [u"C:\\temp\\fileA.shp", u"C:\\temp\\fileB.shp", u"C:\\temp\\fileC.shp"]
with open(r'C:\temp\myfile.csv', "wb") as f:
wr = csv.writer(f, delimiter=',', quoting=csv.QUOTE_NONE)
wr.writerows([li])
Which yields a list of files on the same row:
C:\temp\fileA.shp,C:\temp\fileB.shp,C:\temp\fileC.shp
How can I tweak this so that the pathnames are each on their own row? The following is what I am after:
C:\temp\fileA.shp
C:\temp\fileB.shp
C:\temp\fileC.shp
Easy just need to add \n witch means new
import csv
li = [u"C:\\temp\\fileA.shp", u"C:\\temp\\fileB.shp", u"C:\\temp\\fileC.shp"]
with open(r'C:\temp\myfile.txt', "wb") as f:
wr = csv.writer(f + '\n', delimiter=',', quoting=csv.QUOTE_NONE)
wr.writerows([li])
So now f will be printed + \n (new line)
I have some data that needs to be written to a CSV file. The data is as follows
A ,B ,C
a1,a2 ,b1 ,c1
a2,a4 ,b3 ,ct
The first column has comma inside it. The entire data is in a list that I'd like to write to a CSV file, delimited by commas and without disturbing the data in column A. How can I do that? Mentioning delimiter = ',' splits it into four columns on the whole.
Just use the csv.writer from the csv module.
import csv
data = [['A','B','C']
['a1,a2','b1','c1']
['a2,a4','b3','ct']]
fname = "myfile.csv"
with open(fname,'wb') as f:
writer = csv.writer(f)
for row in data:
writer.writerow(row)
https://docs.python.org/library/csv.html#csv.writer
No need to use the csv module since the ',' in the first column is already part of your data, this will work:
with open('myfile.csv', 'w') as f:
for row in data:
f.write(', '.join(row))
f.write('\n')
You could try the below.
Code:
import csv
import re
with open('infile.csv', 'r') as f:
lst = []
for line in f:
lst.append(re.findall(r',?(\S+)', line))
with open('outfile.csv', 'w', newline='') as w:
writer = csv.writer(w)
for row in lst:
writer.writerow(row)
Output:
A,B,C
"a1,a2",b1,c1
"a2,a4",b3,ct