一道小题,用Python将字符串里的英文双引号转换成中文双引号
要求:高效算法
输入参数:基本原则"这"是一个"测试用例",完毕
结果:基本原则“这”是一个“测试用例”,完毕
以下来自: https://gist.github.com/1350419
要求:高效算法
输入参数:基本原则"这"是一个"测试用例",完毕
结果:基本原则“这”是一个“测试用例”,完毕
以下来自: 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 te