Python to replace a symbol between between 2 words in a quote


trying to replace <strong>comma</strong> between 2 words <strong>Durango and PC</strong> in the second line by <strong>&</strong> and then remove the quotes " as well. Same for third line with <strong>Orbis and PC</strong> 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 ... ... ...

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.

How to achieve this, any suggestion? Learning Python.


Use a <a href="https://docs.python.org/3/library/re.html" rel="nofollow">regular expression</a> for this. Write a regular expression that catches your string, and then use it to replace it.

It should look something like this:

\"(\w*),\ (\w*)\"

This will catch your <em>Durango</em> and <em>PC</em>, and map both of them into two groups which you later use when you replace them.


Something like this? Add proper if() if you want to switch comma to ampersand only in second line:

This code goes trough all lines with ..., "..., ...", ... => ..., ... & ..., ...

import re expr = r'2,Kenny Chong,Core Tech - Rendering,"Durango, PC",55,Reopened' expr2 = re.findall('"(.*?)"', expr) if len(expr2)!=0: expr3 = re.split('"',expr) expr4 = expr3[0]+expr3[1].replace(","," &")+expr3[2] print(expr4)


Something like below:

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


  • How to check if stdin is readable in TCL?
  • synchronizing SDK with Windows 10 update and using WinRT with Standard C++
  • What is the URL option to display a Mobile XPage App with iPhone theme?
  • R: Knitr gives error for SQL-chunk
  • AngularJS RESTAPI call throwing error
  • Convert text file to plink PED and MAP format
  • Update Activity data from service when its paused
  • Mediafire developer doesn't show me Create new app
  • How to use keywords as column names in Derby
  • Pulling Columns from Excel
  • stray characters coming after splitting the list in tcl
  • Hibernate exception on encountering mysql := operator
  • Unable to get a dynamically generated content from a webpage
  • Passing multiple parameters from controller to factory in angularjs
  • Escaping user data, without magic quotes
  • Need help replacing single quotes in bash script
  • Does Python have an equivalent of Perl's qq?
  • How to execute a shell script through python
  • Serializing (and deserializing) 'complex' Rails objects with JSON
  • MongoDB: How to Sort a Query Before Updating
  • QT: set stylesheet for a QMenu object
  • Converting nested html to bbCode quote tags
  • Groovy install HTTPBuilder now that Codehaus shutdown?
  • constexpr vs const vs constexpr const
  • Where and How To Define An Application Property? - JHIpster
  • Get row for each user where the count of a value in a column is maximum
  • getUserMedia results in TrackStartError in Chrome
  • How to prepare a C++ string for sql query
  • 302 Redirect from http to https in Android using Dropbox short Hyperlinks
  • Finding number of samples in a .wav header
  • How to crop a mp3 in ASP.NET + C#?
  • PyYaml parses '9:00' as int
  • XSD assert not recognised
  • C function strchr - How to calculate the position of the character?
  • Trying to get the char code of ENTER key
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Can I make an Android app that runs a web view in Chrome 39?
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster