Python在引号中2个单词之间替换符号 [英] Python to replace a symbol between between 2 words in a quote

查看:171
本文介绍了Python在引号中2个单词之间替换符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在第二行的 Durango和PC 两个单词之间用& 替换逗号,然后也删除引号。与 Orbis和PC 的第三行相同,我想使用Python保留其余的行:

trying to replace comma between 2 words Durango and PC in the second line by & and then remove the quotes " as well. Same for third line with Orbis and PC I would like to retain the rest of the lines using Python:

2,SIN-Rendering,Core Tech - Rendering,PC,147,Reopened
2,Kenny Chong,Core Tech - Rendering,"Durango, PC",55,Reopened
3,SIN-Audio,AAA - Audio,"Orbis, PC",13,Open
...
... 
...

像这些一样,我的样本中可能有100行,因此预期输出为:

Like these, there can be 100 lines in my sample. So the expected output is:

2,SIN-Rendering,Core Tech - Rendering,PC,147,Reopened
2,Kenny Chong,Core Tech - Rendering, Durango & PC,55,Reopened
3,SIN-Audio,AAA - Audio, Orbis & PC,13,Open
...
...
...

到目前为止,我可以考虑逐行阅读,然后如果该行包含引号,则不加任何字符

So far, I could think of reading line by line and then if the line contains quote replace it with no character but then replacement of symbol inside is something I am stuck with.

如何实现这一点,有什么建议吗?学习Python。

How to achieve this, any suggestion? Learning Python.

推荐答案

类似于以下内容:

st = "2,Kenny Chong,Core Tech - Rendering,\"Durango, PC\",55,Reopened"
res = re.sub(r'\"(.*),\ (.*)\"',lambda x : (" & ").join(re.findall(r'\"(.*),\ (.*)\"', st)[0]),st)

这篇关于Python在引号中2个单词之间替换符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆