File: CSVJoiner.py

package info (click to toggle)
pastewebkit 1.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 536 kB
  • ctags: 662
  • sloc: python: 3,346; makefile: 51
file content (21 lines) | stat: -rw-r--r-- 607 bytes parent folder | download | duplicates (5)
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)