#!/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)
基本原则“这”是一个“测试用例”,完毕