Remove commas in a string, surrounded by a comma and double quotes / Python

2024/10/3 23:29:48

I've found some similar themes on stackoverflow, but I'm newbie to Python and Reg Exps.

I have a string

,"Completely renovated in 2009, the 2-star Superior Hotel Ibis BerlinMesse, with its 168 air-conditioned rooms, is located right next toBerlin's ICC and exhibition center. All rooms have Wi-Fi, and you cansurf the Internet free of charge at two iPoint-PCs in the lobby. Weprovide a 24-hour bar, snacks and reception service. Enjoy ourbreakfast buffet from 4am to 12pm on the 8th floor, where you have afantastic view across Berlin. You will find free car parking directlynext to the hotel.",

A pattern should be like: comma, double quote|any text with commas |double quote, comma. I need to replace commas in double quotes, for example with @ character. Which reg exp pattern should I use?

I tried this :

r',"([.*]*,[.*]*)*",' 

with different variations, but it doesn't work.

Thanks for the answers, the problem was solved.

Answer

If all you need to do is replace commas with the @ character you should look into doing a str_replace rather than regex.

str_a = "Completely renovated in 2009, the 2-star Superior Hotel Ibis Berlin Messe, with its 168 air-conditioned rooms, is located right next to Berlin's ICC and exhibition center. All rooms have Wi-Fi, and you can surf the Internet free of charge at two iPoint-PCs in the lobby. We provide a 24-hour bar, snacks and reception service. Enjoy our breakfast buffet from 4am to 12pm on the 8th floor, where you have a fantastic view across Berlin. You will find free car parking directly next to the hotel."str_a = str_a.replace('","', '@') #commas inside double quotes
str_a = str_a.replace(',', '@') #replace just commasprint str_a

Edit: Alternatively you could make a list of what you want to replace, then loop through it and perform the replacement. Ex:

to_replace = ['""', ',', '"']str_a = "Completely renovated in 2009, the 2-star Superior Hotel Ibis Berlin Messe, with its 168 air-conditioned rooms, is located right next to Berlin's ICC and exhibition center. All rooms have Wi-Fi, and you can surf the Internet free of charge at two iPoint-PCs in the lobby. We provide a 24-hour bar, snacks and reception service. Enjoy our breakfast buffet from 4am to 12pm on the 8th floor, where you have a fantastic view across Berlin. You will find free car parking directly next to the hotel."for a in to_replace:str_a = str_a.replace(a, '@')print str_a
https://en.xdnf.cn/q/70676.html

Related Q&A

I need help making a discord py temp mute command in discord py

I got my discord bot to have a mute command but you have to unmute the user yourself at a later time, I want to have another command called "tempmute" that mutes a member for a certain number…

How to clip polar plot in pylab/pyplot

I have a polar plot where theta varies from 0 to pi/2, so the whole plot lies in the first quater, like this:%pylab inline X=linspace(0,pi/2) polar(X,cos(6*X)**2)(source: schurov.com) Is it possible b…

Cython and c++ class constructors

Can someone suggest a way to manipulate c++ objects with Cython, when the c++ instance of one class is expected to feed the constructor of another wrapped class as described below? Please look at th…

How to share state when using concurrent futures

I am aware using the traditional multiprocessing library I can declare a value and share the state between processes. https://docs.python.org/3/library/multiprocessing.html?highlight=multiprocessing#s…

Does IronPython implement python standard library?

I tried IronPython some time ago and it seemed that it implements only python language, and uses .NET for libraries. Is this still the case? Can one use python modules from IronPython?

finding the last occurrence of an item in a list python

I wish to find the last occurrence of an item x in sequence s, or to return None if there is none and the position of the first item is equal to 0This is what I currently have:def PositionLast (x,s):co…

pandas cut a series with nan values

I would like to apply the pandas cut function to a series that includes NaNs. The desired behavior is that it buckets the non-NaN elements and returns NaN for the NaN-elements.import pandas as pd numbe…

Using Selenium with PyCharm CE

Im trying to use Selenium with PyCharm CE. I have installed Selenium using pip install Selenium and Im able to use it via the terminal however when I try to use it with PyCharm I get an import error Im…

Reusing generator expressions

Generator expressions is an extremely useful tool, and has a huge advantage over list comprehensions, which is the fact that it does not allocate memory for a new array.The problem I am facing with gen…

ModuleNotFoundError: No module named librosa

Currently I am working on voice recognition where I wanted to use Librosa library. I install librosa with the command on ubuntu: conda install -c conda-forge librosaBut when I run the code I got the fo…