Putting Quotes to each string of a line

Question:

I am just trying to put quotes to each string in a line (I have a file with lines like this and look like):

dog, monkey, elephant
monkey, dog, cat
fish, bird

and I want to make it like this(put each string in a quote):

'dog', 'monkey', 'elephant'
'monkey', 'dog', 'cat'
'fish', 'bird'

Asked By: abcabc

||

Answers:

please not that text values need to be in quotes, otherwise pythin cannot see the difference to a variable name.

try this

S1 = set(["abc"])
S2 = set(["efg"])
sets = [S1, S2]

result = [[f"'{v}'" for v in S] S for S in sets]

print(result)
Answered By: Klops

This can be accomplished with something like this (making a few assumptions about your program):

filename: str = 'filename.txt'

with open(filename, 'r') as f:
    for line in f:
        print(', '.join(f''{a}'' for a in line.rstrip('nr').split(', ')))
Answered By: VoidTwo
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.