python字符串处理问题: 如何将若干字符串前面的括弧及里面的内容转移拼接到字符串尾部? Python字符串拼接的几种方法整理

\u6c42python\u5b57\u7b26\u4e32\u5904\u7406\u95ee\u9898\uff1a \u600e\u4e48\u5c06\u82e5\u5e72\u5b57\u7b26\u4e32\u524d\u9762\u7684\u62ec\u5f27\u53ca\u91cc\u9762\u7684\u5185\u5bb9\u8f6c\u79fb\u62fc\u63a5\u5230\u5b57\u7b26\u4e32\u5c3e\u90e8\uff1f

#coding:gbk
import re
import sys

NUM_PAT = re.compile(r'\uff08.*?\uff09')

if __name__ == "__main__":
fp = open(sys.argv[1])
srcContend = fp.readlines()
fp.close()

fp = open(sys.argv[1], 'w')
for line in srcContend:
result = NUM_PAT.search(line)
if result != None:
fp.write(NUM_PAT.subn('', line)[0].strip() + result.group(0) + '\n')
else:
fp.write(line)
fp.close()

1)\u6ce8\u91ca\u7684\u4ee3\u7801\u4e0d\u8981\u5220
2)\u8fd0\u884c\u7684\u683c\u5f0f\u662fXXX.py a.txt , \u5176\u4e2d,XXX.py\u662f\u4f60\u8fd9\u4e2apy\u7684\u6587\u4ef6\u540d, a.txt\u662f\u8981\u5904\u7406\u7684\u6587\u672c\u6587\u4ef6, \u5904\u7406\u4e4b\u540e\u7684\u7ed3\u679c\u4e5f\u662f\u653e\u5728a.txt\u91cc\u9762,\u6240\u4ee5\u4f60\u6700\u597d\u5907\u4efd\u4e00\u6b21\u4f60\u7684a.txt

1\u3001\u76f8\u52a0
website = 'python' + 'tab' + '.com'
2\u3001%
'my name is %s\uff0cnow %d years old' % ('liming',27)
3\u3001{}.format
'myname is {0},now {1} years old'.format('liming','27')

#!python3.2

#定义转换函数
def convert(s):
index1=s.index('(')
index2=s.index(')')
index3=s.index('。')
return s[index2+1:index3]+s[:index2+1]+s[index3:]

#您的列表ls,请自行填写
ls=[]

#开始转换
for s in ls:
print(convert(s))

不确定你的句号是不是这样处理。我保留最后一个字符位置不变。

代码见下图:



。。。。。。。按空格

扩展阅读:python if ... python教程 ... python字符串格式化 ... python字符串转换为数字 ... python入门之字符串处理 ... python字符串逆序输出 ... python编程经典例题 ... 头歌平台python字符串处理 ... python字符串分割split ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网