Skip to content Skip to sidebar Skip to footer

Remove Quotes Holding 2 Words And Remove Comma Between Them

Following up on Python to replace a symbol between between 2 words in a quote Extended input and expected output: trying to replace comma between 2 words Durango and PC in the seco

Solution 1:

So, by treating the input file as a .csv we can easily turn the lines into something easy to work with.

For example,

2,Kenny Chong,Core Tech - Rendering, Durango & PC,55,Reopened

is read as:

['2', 'Kenny Chong', 'Core Tech - Rendering', 'Durango, PC', '55', 'Reopened']

Then, by replacing all instances of , with _& (space) we would have the line:

['2', 'Kenny Chong', 'Core Tech - Rendering', 'Durango & PC', '55', 'Reopened']

And it replaces multiple instances of ,s within a line, and when finally writing we no longer have the original double quotes.

Here is the code, given that in.txt is your input file and it will write to out.txt.

import csv

withopen('in.txt') as infile:
    reader = csv.reader(infile)

    withopen('out.txt', 'w') as outfile:
        for line in reader:
            line = list(map(lambda s: s.replace(',', ' &'), line))
            outfile.write(','.join(line) + '\n')

The fourth line is outputted as:

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

Solution 2:

Please check this once: I could not find a single expression that could do this. So did it in a bit elaborate way. Will update if I can find a better way(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)

Post a Comment for "Remove Quotes Holding 2 Words And Remove Comma Between Them"