英文:
remove string between comma if contains None
问题
我想要从我的字符串中删除 "col=None",结果应该如下(使用Python):
my_str = ""energie='E', statut_contrat='CC', occurrence='4'""
谢谢。
英文:
I have this string :
my_str = "energie='E', Offre=None, segment='X', mode_de_paiement=None, statut_contrat='CC', statut_fel=None, fmr=None, payeur_divergent=None, type_de_conducteur=None, compte_duale_rv=None, plan_mensualisation=None, adm=None, multisite=None, occurrence='4'"
i want to remove from my string, col=None so the result would be using Pyhton
my_str = "energie='E', statut_contrat='CC', occurrence='4'"
Thanks
答案1
得分: 0
Sure, here is the translated code part:
my_str = "energy='E', Offer=None, segment='X', payment_mode=None, contract_status='CC', fel_status=None, fmr=None, divergent_payer=None, driver_type=None, dual_rv_account=None, installment_plan=None, adm=None, multisite=None, occurrence='4'"
my_str = ",".join([x for x in my_str.split(",") if not x.endswith("=None")]).strip()
英文:
my_str = "energie='E', Offre=None, segment='X', mode_de_paiement=None, statut_contrat='CC', statut_fel=None, fmr=None, payeur_divergent=None, type_de_conducteur=None, compte_duale_rv=None, plan_mensualisation=None, adm=None, multisite=None, occurrence='4'"
my_str = ",".join([x for x in my_str.split(",") if not x.endswith("=None")]).strip()
答案2
得分: 0
要删除字符串中所有包含 "=None" 的元素,您可以使用列表推导和内置于Python中的字符串方法。
您可以使用以下代码将原始字符串 "my_str" 拆分为项:
items = [item for item in my_str.split(',') if '=None' not in item]
这将创建一个包含您所需字符串的列表。您可以使用Python中的 join
和 replace
方法将其转换回字符串:
my_str = ','.join(items)
结果将是您初始字符串中不包含 "None" 的所有键-值对。
英文:
To delete all the elements in your string that contain =None
, you can use list comprehension and string methods that are built into Python.
You can split your original string my_str
into items using
items = [item for item in my_str.split(',') if not '=None' in item]
This will create a list of all the strings you need. You can convert this back into a string using the join
and replace
methods in python
my_str = ','.join(items)
The result will be all the key-value pairs in your initial string that do not contain None
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论