Do not use this for SQL query generation. Use the database driver SQL parameters instead. You cannot hope to properly escape your way out of SQL injection attacks otherwise.
If you need to use a WHERE .. IN .. test, generate placeholders:
query = 'SELECT * FROM table WHERE column IN ({})'.format(','.join(['%s'] * len(lst)))
cursor.execute(query, lst)
For everything else, use a list comprehension to add the quotes to the values, then join the results with commas:
', '.join(['"{}"'.format(value) for value in lst])
Demo:
>>> lst = ['John','Jack','Martin']
>>> ', '.join(['"{}"'.format(value) for value in lst])
'"John", "Jack", "Martin"'
>>> print ', '.join(['"{}"'.format(value) for value in lst])
"John", "Jack", "Martin"
This will consistently use " double quotes; simply use "'{}'" as the template if you must have single quotes instead.
Do not use this for SQL query generation. Use the database driver SQL parameters instead. You cannot hope to properly escape your way out of SQL injection attacks otherwise.
If you need to use a WHERE .. IN .. test, generate placeholders:
query = 'SELECT * FROM table WHERE column IN ({})'.format(','.join(['%s'] * len(lst)))
cursor.execute(query, lst)
For everything else, use a list comprehension to add the quotes to the values, then join the results with commas:
', '.join(['"{}"'.format(value) for value in lst])
Demo:
>>> lst = ['John','Jack','Martin']
>>> ', '.join(['"{}"'.format(value) for value in lst])
'"John", "Jack", "Martin"'
>>> print ', '.join(['"{}"'.format(value) for value in lst])
"John", "Jack", "Martin"
This will consistently use " double quotes; simply use "'{}'" as the template if you must have single quotes instead.
You could use repr to make each string valid Python input:
lst = ['John','Jack','Martin']
output = ','.join(map(repr,lst))
c# - How to convert List<string> into String of Comma Separated Quotes from List - Stack Overflow
linux - Turning separate lines into a comma separated list with quoted entries - Unix & Linux Stack Exchange
Converting a list into comma separated and add quotes in python - Stack Overflow
python - convert a list into a single string consisting with double quotes and separated by comma - Stack Overflow
Videos
You can add quotes with sed and then merge lines with paste, like that:
sed 's/^\|$/"/g'|paste -sd, -
If you are running a GNU coreutils based system (i.e. Linux), you can omit the trailing '-'.
If you input data has DOS-style line endings (as @phk suggested), you can modify the command as follows:
sed 's/\r//;s/^\|$/"/g'|paste -sd, -
awk:
awk 'BEGIN { ORS="" } { print p"'"'"'"$0"'"'"'"; p=", " } END { print "\n" }' /path/to/list
Alternative with less shell escaping and therefore more readable:
awk 'BEGIN { ORS="" } { print p"\047"$0"\047"; p=", " } END { print "\n" }' /path/to/list
Output:
'd3heatmap', 'data.table', 'ggplot2', 'htmltools', 'htmlwidgets', 'metricsgraphics', 'networkD3', 'plotly', 'reshape2', 'scales', 'stringr'
Explanation:
The awk script itself without all the escaping is BEGIN { ORS="" } { print p"'"$0"'"; p=", " } END { print "\n" }. After printing the first entry the variable p is set (before that it's like an empty string). With this variable p every entry (or in awk-speak: record) is prefixed and additionally printed with single quotes around it. The awk output record separator variable ORS is not needed (since the prefix is doing it for you) so it is set to be empty at the BEGINing. Oh and we might our file to END with a newline (e.g. so it works with further text-processing tools); should this not be needed the part with END and everything after it (inside the single quotes) can be removed.
If you have Windows/DOS-style line endings (\r\n), you have to convert them to UNIX style (\n) first. To do this you can put tr -d '\015' at the beginning of your pipeline:
tr -d '\015' < /path/to/input.list | awk […] > /path/to/output
(Assuming you don't have any use for \rs in your file. Very safe assumption here.)
Alternatively, simply run dos2unix /path/to/input.list once to convert the file in-place.
I see you don't want to just prepend/append the needed substrings(the single quote and white space) to the crucial string.
Here's a trick with str.replace() function:
lst = ['1','2','3']
result = "' - '".replace('-', ",".join('"{}"'.format(i) for i in lst))
print(result)
The output:
' "1","2","3" '
"' - '"- acts as a placeholder
Or the same with additional str.format() function call:
result = "' {} '".format(",".join('"{}"'.format(i) for i in lst))
list = ['1','2','3', '5']
result = "' - '".replace('-', ",".join('"{}"'.format(i) for i in list))
# "' - '" - acts as a placeholder
print(result) # ' "1","2","3","5" '