删除包含 None 的逗号之间的字符串。

huangapple go评论66阅读模式
英文:

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中的 joinreplace 方法将其转换回字符串:

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

huangapple
  • 本文由 发表于 2023年3月21日 00:11:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定