Strip any comma from each field that you write to the row, eg:
label2 = ''.join(label2.split(','))
facidcsv = ''.join(facidcsv.split(','))
County = ''.join(County.split(','))
row = "{},{},{}\n".format(label2,facidcsv,County)
Generalized to format a row with any number of fields:
def format_row(*fields):
row = ''
for field in fields:
if row:
row = row + ', ' + ''.join(field.split(','))
else:
row = ''.join(field.split(','))
return row
label2 = 'label2, label2'
facidcsv = 'facidcsv'
county = 'county, county'
print(format_row(label2, facidcsv, county))
wildcsv.write(format_row(label2, facidcsv, county))
Output
label2 label2, facidcsv, county county
As @TomaszPlaskota and @quapka allude to in the comments, Python's csv
writers
and readers
by default write/read csv fields that contain a delimiter with a surrounding '"'
. Most applications that work with csv files follow the same format. So the following is the preferred approach if you want to keep the commas in the output fields:
import csv
label2 = 'label2, label2'
facidcsv = 'facidccv'
county = 'county, county'
with open('out.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow((label2, facidcsv, county))
out.csv
"label2, label2",facidccv,"county, county"