split by comma and ignore if comma in quotes a data from List of Tuples – Python

Question:

I have a list of tuples.

lt = [('051623', 'O143', '1.23', '2023-05-16T18:30:00', '1M allen', 'millan'), ('051623', 'O207', '1.23', '2023-05-16T18:35:00', 'nM Mn, 4nM, 35uM Fe', 'wilo')]

Need to convert to csv string by comma separated but while split the commas within quotes to be ignored: Expected is below.

'051623','O143','1.23','2023-05-16T18:30:00','1M allen','millan'
'051623', 'O207', '1.23', '2023-05-16T18:35:00','nM Mn, 4nM, 35uM Fe','wilo'

I tried but not working:

csv_string = 'n'.join(",".join([element if element else 'None' for element in tup]) for tup in lt)
Asked By: M Raj

||

Answers:

You can use the csv library:

lt = [
  ('051623', 'O143', '1.23', '2023-05-16T18:30:00', '1M allen', 'millan'), 
  ('051623', 'O207', '1.23', '2023-05-16T18:35:00', 'nM Mn, 4nM, 35uM Fe', 'wilo'),
]

import io
import csv


# Allows saving the csv output to a string instead of a file
f = io.StringIO(newline='')    

writer = csv.writer(
    f,
    quotechar="'",
    quoting=csv.QUOTE_ALL,
)
writer.writerows(lt)

# `getvalue()` returns a string containing the csv output
print(f.getvalue())

It outputs

'051623','O143','1.23','2023-05-16T18:30:00','1M allen','millan'
'051623','O207','1.23','2023-05-16T18:35:00','nM Mn, 4nM, 35uM Fe','wilo'
Answered By: enzo

If you’re just trying to recompose the lines as csv data without actually writing to CSV, you can use the following, which will wrap every individual value in double quotes

lines=[]
for tup in lt:
    lines.append('"' + '","'.join(tup)+'"')

print(lines)
# [
# '"051623","O143","1.23","2023-05-16T18:30:00","1M allen","millan"',
# '"051623","O207","1.23","2023-05-16T18:35:00","nM Mn, 4nM, 35uM Fe","wilo"'
# ]

Or if you only want to wrap the cells that contain commas:

lines=[]
for tup in lt:
    line=''
    for el in tup:
        if ',' in el:
            el = '"'+el+'"'
        line += el + ','
    lines.append(line[:-1]) # Strip the extra , at the end

print(lines)
# [
#   '051623,O143,1.23,2023-05-16T18:30:00,1M allen,millan',
#   '051623,O207,1.23,2023-05-16T18:35:00,"nM Mn, 4nM, 35uM Fe",wilo'
# ]
Answered By: nigh_anxiety

Here is an example.

s = 'n'.join([','.join([f"'{y}'" for y in x]) for x in lt])

Output

'051623','O143','1.23','2023-05-16T18:30:00','1M allen','millan'
'051623','O207','1.23','2023-05-16T18:35:00','nM Mn, 4nM, 35uM Fe','wilo'

Additionally, you’ll want to escape any single quotation-marks within the text.

s = 'n'.join([','.join([f"'{y}'" for y in [z.replace("'", r"'") for z in x]]) for x in lt])
Answered By: Reilas

My code iterates through the list lt, formats each element in the tuples by adding single quotes around them, and joins them with commas. as specified in the your question:

lt = [
    ("051623", "O143", "1.23", "2023-05-16T18:30:00", "1M allen", "millan"),
    ("051623", "O207", "1.23", "2023-05-16T18:35:00", "nM Mn, 4nM, 35uM Fe", "wilo"),
]
for words in lt:
    formatted_words = []
    for word in words:
        formatted_word = "'{}'".format(word)
        formatted_words.append(formatted_word)
    formatted_lt = ",".join(formatted_words)
    print(formatted_lt)
Answered By: XMehdi01
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.