python将字符串中英文单双引号改为中文单双引号

前端之家收集整理的这篇文章主要介绍了python将字符串中英文单双引号改为中文单双引号前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

import re

str = 'dsf"safd"sfas\'fwe\'rrw\'erw\'ewwew"rsdr"sdsd'
print(str)

# 将成对的英文双引号改为成对的中文双引号
pattern = re.compile(r'"(.*?)"')
result = pattern.findall(str)
for l in result:
  str = str.replace('"{}"'.format(l),'“{}”'.format(l))
# 将成对的英文单引号改为成对的中文单引号
pattern = re.compile(r"'(.*?)'")
result = pattern.findall(str)
for l in result:
  str = str.replace("'{}'".format(l),"‘{}’".format(l))


# 将单独的单双引号替换为空
str = str.replace("'","")
str = str.replace('"',"")

print(str)

 

猜你在找的Python相关文章