Skip to content

Instantly share code, notes, and snippets.

@nicelifeBS
Created March 12, 2018 09:13
Show Gist options
  • Save nicelifeBS/d22a8ab5247ad1503b4d3d8dc6a3e58f to your computer and use it in GitHub Desktop.
Save nicelifeBS/d22a8ab5247ad1503b4d3d8dc6a3e58f to your computer and use it in GitHub Desktop.
Replace double quotes with single quotes in string
import re
def replace(obj):
print(obj.groups())
if obj.group(0) == '\'':
return '"'
if obj.group(0) == '"':
return '\''
if obj.group(0) == '"""':
return '"""'
def main():
test_string = r'''
"""Doc string"""
list = ["string", "another string"]
string = "blabla"
nested = "Hello 'World'"
escaped = "Some \"escaped\""
'''
regex = re.compile(r'''
(\"\"\")|((?<!\\)\")|(')
''', re.VERBOSE
)
for line in test_string.split('\n'):
result = regex.sub(replace, line)
print(result)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment