Created
March 12, 2018 09:13
-
-
Save nicelifeBS/d22a8ab5247ad1503b4d3d8dc6a3e58f to your computer and use it in GitHub Desktop.
Replace double quotes with single quotes in string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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