本文介绍了如何从变量中删除某些字符?(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个名为 data 的变量.这个数据变量包含所有这些数据,我需要删除其中的某些部分,同时保留大部分数据.假设我需要删除此数据变量中的所有,"(逗号).我将如何编写一个脚本来分析该数据然后删除这些逗号?
Let's suppose I have a variable called data. This data variable has all this data and I need to remove certain parts of it while keeping most of it. Let's say I needed to remove all the ',' (commas) in this data variable. How would I write a script that would analyze that data and then remove those commas?
代码示例:
data = '''
data,data,data,data,data,data
data,data,data,data,data,data
'''
推荐答案
只需替换它们:
data = data.replace(',', '')
如果您有更多字符,请尝试使用 .translate()
:
If you have more characters, try using .translate()
:
data = data.translate(None, ',.l?asd')
这篇关于如何从变量中删除某些字符?(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!