紧跟Python to replace a symbol between between 2 words in a quote
扩展的输入和预期的输出:
尝试在第二行的 Durango和PC 两个单词之间用&替换逗号,然后也删除引号“。对于 Orbis和PC 的第三行和第四行的报价,我想要处理“ AAA-Character Tech,SOF-UPI”,有两个单词组合, “ Durango,Orbis,PC”
我想使用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
LTY-168499,[PC][PS4][XB1] Missing textures from Fort Capture NPC face,3,CTU-CharacterTechBacklog,"AAA - Character Tech, SOF - UPIs","Durango, Orbis, PC",29,Waiting For
...
...
...
像这些一样,我的样本中可能有100行。因此,预期的输出为:
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
LTY-168499,[PC][PS4][XB1] Missing textures from Fort Capture NPC face,3,CTU-CharacterTechBacklog,AAA - Character Tech & SOF - UPIs,Durango, Orbis & PC,29,Waiting For
...
...
...
到目前为止,我可以考虑逐行阅读,然后如果该行包含引号,则不使用任何字符替换它,但是替换里面的符号是我所坚持的。
这是我现在拥有的:
for line in lines:
expr2 = re.findall('"(.*?)"', line)
if len(expr2)!=0:
expr3 = re.split('"',line)
expr4 = expr3[0]+expr3[1].replace(","," &")+expr3[2]
print >>k, expr4
else:
print >>k, line
但是它不考虑第四行的情况吗?也可以有3个以上的连击。例如。
3,SIN-Audio,"AAA - Audio, xxxx, yyyy","Orbis, PC","13, 22",Open
并希望做到这一点
3,SIN-Audio,AAA - Audio & xxxx & yyyy, Orbis & PC, 13 & 22,Open
如何实现这一目标,有什么建议吗?学习Python。
答案 0 :(得分:2)
因此,通过将输入文件视为.csv
,我们可以轻松地将行变成易于使用的内容。
例如
2,Kenny Chong,Core Tech - Rendering, Durango & PC,55,Reopened
读取为:
['2', 'Kenny Chong', 'Core Tech - Rendering', 'Durango, PC', '55', 'Reopened']
然后,通过将,
的所有实例替换为_&
(空格),我们将得到以下一行:
['2', 'Kenny Chong', 'Core Tech - Rendering', 'Durango & PC', '55', 'Reopened']
它替换了一行中,
的多个实例,并且在最终写入时,我们不再有原始的双引号。
这里是代码,假设in.txt
是您的输入文件,它将写入out.txt
。
import csv
with open('in.txt') as infile:
reader = csv.reader(infile)
with open('out.txt', 'w') as outfile:
for line in reader:
line = list(map(lambda s: s.replace(',', ' &'), line))
outfile.write(','.join(line) + '\n')
第四行输出为:
LTY-168499,[PC][PS4][XB1] Missing textures from Fort Capture NPC face,3,CTU-CharacterTechBacklog,AAA - Character Tech & SOF - UPIs,Durango & Orbis & PC,29,Waiting For
答案 1 :(得分:0)
请检查一次:我找不到可以执行此操作的单个表达式。这样做也做了一些精心设计。如果我能找到更好的方法,它将进行更新(Python 3)
import re
st = "3,SIN-Audio,\"AAA - Audio, xxxx, yyyy\",\"Orbis, PC\",\"13, 22\",Open"
found = re.findall(r'\"(.*)\"',st)[0].split("\",\"")
final = ""
for word in found:
final = final + (" &").join(word.split(","))+","
result = re.sub(r'\"(.*)\"',final[:-1],st)
print(result)