Python Forum
replace string inside double quotes
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
replace string inside double quotes
#1
I am trying to replace commas with a "^" in a string that is already quoted. Below you can see input and output. However my code does not replace the string. 

Any help would be appreciated.

Input:
Output:
Id,Category,Description,Date 1,Test,Red Cars,02/12/2017 2,Test,Blue Cars,03/01/2017 3,Test,"Green, big cars",01/05/2016
Output should be:
Output:
Id,Category,Description,Date 1,Test,Red Cars,02/12/2017 2,Test,Blue Cars,03/01/2017 3,Test,"Green^ big cars",01/05/2016
import csv

ifile = open('C:/Users/jpilon/Documents/test.csv', 'r')
reader = csv.reader(ifile,delimiter=',')
ofile = open('C:/Users/jpilon/Documents/test_new.csv', 'w')
writer = csv.writer(ofile, delimiter=',')


findlist = ['"*,*"']
replacelist = ['"*^*"']

rep = dict(zip(findlist, replacelist))

def findReplace(find, replace):
   s = ifile.read()
   s = s.replace(find, replace)
   ofile.write(s)

for item in findlist:
   findReplace(item, rep[item])

ifile.close()
ofile.close()
Reply


Messages In This Thread
replace string inside double quotes - by jmpatx - Apr-21-2017, 08:58 PM
RE: replace string inside double quotes - by jmpatx - Apr-24-2017, 03:32 PM
RE: replace string inside double quotes - by nilamo - Apr-24-2017, 03:43 PM
RE: replace string inside double quotes - by jmpatx - Apr-24-2017, 10:29 PM
RE: replace string inside double quotes - by wavic - Apr-25-2017, 04:16 AM
RE: replace string inside double quotes - by jmpatx - Apr-25-2017, 08:18 PM
RE: replace string inside double quotes - by nilamo - Apr-26-2017, 05:08 AM
RE: replace string inside double quotes - by nilamo - Apr-26-2017, 05:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to replace a string with a file (HTML file) tester_V 1 862 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Replace string in a nested Dictianory. SpongeB0B 2 1,322 Mar-24-2023, 05:09 PM
Last Post: SpongeB0B
  Replace with upper(string) WJSwan 7 1,718 Feb-10-2023, 10:28 AM
Last Post: WJSwan
  Need help on how to include single quotes on data of variable string hani_hms 5 2,284 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  Find and Replace numbers in String giddyhead 2 1,333 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,406 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  Replace String with increasing numer [SOLVED] AlphaInc 13 5,341 Aug-07-2021, 08:16 AM
Last Post: perfringo
  Remove single and double quotes from a csv file in 3 to 4 column shantanu97 0 7,130 Mar-31-2021, 10:52 AM
Last Post: shantanu97
  Two types of single quotes Led_Zeppelin 2 1,994 Mar-15-2021, 07:55 PM
Last Post: BashBedlam
  How to replace on char with another in a string? korenron 3 2,436 Dec-03-2020, 07:37 AM
Last Post: korenron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020