sam_com sam_com - 11 days ago 6
Python Question

python write string to a file without quotes

i am trying to write some string to a file but without quotes.
i check other solutions online but none of them worked for me.
so i am reading from a list and trying to write item[0] to a output.
i tried to map and do item[1:-1] but none of them worked
this my code:

for item in sorted(wrong_blocks):
output.write(str(item[0]))


this keep writing to the file:

"word"

but i just want:

word

without quotes

i checked this
Python - how to write strings to file without quotes and spaces?

Answer

You can remove all instances of a given set of characters from either side of a string with str.strip:

>>> s = '''"hello!"'''.strip('!"')
>>> print(s)
hello

Change your output.write call to output.write(str(item[0]).strip('"')) and it will remove any unwanted quotes.