删除字符串中的逗号,用逗号和双引号/ Python包围

Nod*_*iya 7 python regex

我在stackoverflow上发现了一些类似的主题,但我是Python和Reg Exps的新手.

我有一个字符串

"这家二星级高级酒店Ibis Berlin Messe酒店于2009年经过全面翻修,拥有168间空调客房,毗邻柏林的ICC和展览中心.所有客房均配有无线网络连接,您可以免费上网.在大堂的两台iPoint-PC充电.我们提供24小时营业的酒吧,小吃和接待服务.在8楼的凌晨4点到12点享用我们的自助早餐,在那里您可以欣赏到柏林的美景.你会发现免费的停车场就在酒店旁边.",

模式应该是:comma, double quote|any text with commas |double quote, comma.我需要用双引号替换逗号,例如用@字符替换.我应该使用哪种reg exp模式?

我试过这个:

r',"([.*]*,[.*]*)*",' 
Run Code Online (Sandbox Code Playgroud)

有不同的变化,但它不起作用.

谢谢你的答案,问题解决了.

Pet*_*oti 2

如果您需要做的只是用 @ 字符替换逗号,您应该考虑使用 astr_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."

str_a = str_a.replace('","', '@') #commas inside double quotes
str_a = str_a.replace(',', '@') #replace just commas

print str_a
Run Code Online (Sandbox Code Playgroud)

编辑:或者,您可以列出要替换的内容,然后循环遍历并执行替换。前任:

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
Run Code Online (Sandbox Code Playgroud)