1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
import types
def joinCSVFields(fields):
"""
Returns a CSV record (eg a string) from a sequence of fields.
Fields containing commands (,) or double quotes (") are quotes
and double quotes are escaped (""). The terminating newline is
NOT included.
"""
newFields = []
for field in fields:
assert type(field) is types.StringType
if field.find('"')!=-1:
newField = '"' + field.replace('"', '""') + '"'
elif field.find(',')!=-1 or field.find('\n')!=-1 or field.find('\r')!=-1:
newField = '"' + field + '"'
else:
newField = field
newFields.append(newField)
return ','.join(newFields)
|