您的位置:首页 > 编程语言 > Python开发

用Python将字符串里的英文双引号转换成中文双引号

2011-11-09 12:37 1201 查看
一道小题,用Python将字符串里的英文双引号转换成中文双引号

要求:高效算法

输入参数:基本原则"这"是一个"测试用例",完毕

结果:基本原则“这”是一个“测试用例”,完毕

以下来自:https://gist.github.com/1350419
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Author: Feather
FileName: chnquote.py
Date: 11-09-2011
Description: 中文引号处理转换
'''
import operator
import itertools

def convert(sent):
def double_quote_gen():
yield u""
while 1:
yield u"“"
yield u"”"

assert isinstance(sent, unicode)
seg = sent.split('"')
if len(seg) % 2 != 1:
raise RuntimeError('non-balenced quotes!')
newseg = reduce(tuple.__add__, zip(double_quote_gen(), seg ))
newsent = reduce(unicode.__add__, newseg)
return newsent

def test(msg):

print "msg =>", msg
print "convert(msg) =>", convert(msg)

if __name__ == '__main__':
test( u'基本原则"这"是一个"测试用例"' )
# test( u'测试, "这是"一"个"错误测试用例"')
test( u'""""""""""""')


#---------------------------------------------------------------------------------------------------
又一答案:
来自Techparty-Python群:
-猪之哀伤-  12:53:24
>>> import itertools
>>> obj = itertools.cycle(['“','”'])
>>> _obj = lambda x: obj.next()
>>> import re
>>> before = '''基本原则"这"是一个"测试用例",完毕'''
>>> print re.sub(r"['\"]", _obj, before)
基本原则“这”是一个“测试用例”,完毕
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息