python英文版怎么改成中文版_用Python将字符串里的英文双引号转换成中文双引号...

#!/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)

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值