sam_com sam_com - 1 year ago 314
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):

this keep writing to the file:


but i just want:


without quotes

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

Answer Source

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)

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download