如何从Python中删除包含字符串的字符?

如何从Python中删除包含字符串的字符?,python,string,Python,String,我正在处理一大组csv(表格),我需要删除包含字符的单元格并保留数字单元格 比如说 p1 p2 p3 p4 p5 dcf23e 2322 acc41 4212 cdefd 所以在本例中,我只想删除dcf23e、acc41和cdefd。删除这些字符串后,我希望将它们保留为空单元格 我该怎么做?提前谢谢 我试过的代码是,这段代码删除了字符串中的字符,但问题是,如果字符串是23cdgf2,它会生成一个不是我想要的字符串232。删除所

我正在处理一大组csv(表格),我需要删除包含字符的单元格并保留数字单元格

比如说

   p1     p2      p3       p4      p5
 dcf23e   2322   acc41   4212     cdefd
所以在本例中,我只想删除dcf23e、acc41和cdefd。删除这些字符串后,我希望将它们保留为空单元格

我该怎么做?提前谢谢

我试过的代码是,这段代码删除了字符串中的字符,但问题是,如果字符串是23cdgf2,它会生成一个不是我想要的字符串232。删除所有字符后,当我尝试将字符串转换为int进行计算时,有些字符串变成了小数,因为有些字符串有123def.24->123.24

temp = ''.join([c for c in temp if c in '1234567890.']) # Strip all non-numeric characters
# Now converting strings to integers for calculations, Using function to use   int() , because of the blank spaces cannot be converted to int
def mk_int(s):
    s = s.strip()
    return int(s) if s else 0
mk_int(temp)
print(temp)
使用
regex

import re
def covert_string_to_blank(_str):
    return ['' if re.findall("[a-zA-Z]+", c) else c for c in _str.split()]
或者使用
isalpha

def convert_string_to_blank(_str):
    return ['' if any(c.isalpha() for c in s) else s for s in _str.split()]
使用
regex

import re
def covert_string_to_blank(_str):
    return ['' if re.findall("[a-zA-Z]+", c) else c for c in _str.split()]
或者使用
isalpha

def convert_string_to_blank(_str):
    return ['' if any(c.isalpha() for c in s) else s for s in _str.split()]

编译正则表达式以获得性能,并拆分字符串以获得正确性

import re
regex = re.compile(r'.*\D+.*')
def my_parse_fun(line):
    return [regex.sub('', emt) for emt in line.split()]

根据AbhiP的回答,你也可以

[val if val.isdigit() else '' for val in line.split()]

编译正则表达式以获得性能,并拆分字符串以获得正确性

import re
regex = re.compile(r'.*\D+.*')
def my_parse_fun(line):
    return [regex.sub('', emt) for emt in line.split()]

根据AbhiP的回答,你也可以

[val if val.isdigit() else '' for val in line.split()]

我会使用一个简单的设置来进行快速测试

a = 'dcf23e   2322   acc41   4212     cdefd'
cleaned_val = lambda v: v if v.isdigit() else ''
[cleaned_val(val) for val in a.split()]
如果字符串是有效的数字,它将给出结果,否则将在其位置显示空字符串

['''2322','''4212','']

但是,这仅提供字符串。如果要将值转换为整数(将错误的值替换为0),请更改lambda:

convert_to_int = lambda v: int(v) if v.isdigit() else 0

[convert_to_int(val) for val in a.split()]
您的新结果将是所有有效整数:

[0,2322,0,4212,0]


我会使用一个简单的设置来进行快速测试

a = 'dcf23e   2322   acc41   4212     cdefd'
cleaned_val = lambda v: v if v.isdigit() else ''
[cleaned_val(val) for val in a.split()]
如果字符串是有效的数字,它将给出结果,否则将在其位置显示空字符串

['''2322','''4212','']

但是,这仅提供字符串。如果要将值转换为整数(将错误的值替换为0),请更改lambda:

convert_to_int = lambda v: int(v) if v.isdigit() else 0

[convert_to_int(val) for val in a.split()]
您的新结果将是所有有效整数:

[0,2322,0,4212,0]


您是否使用
try
语句尝试了
for
循环

temp = ['dcf23e','2322','acc41','4212','cdefd']
    index = 0
    for element in temp:
        try:
            element+1
        except:
            del temp[index]
        index = index+1
    print temp
或者,如果要将值转换为
int
元素,可以编写以下代码:

temp = ['dcf23e','2322','acc41','4212','cdefd']
    index = 0
    for element in temp:
        try:
            element+1
        except:
            temp[index] = 0
        index = index+1
    print temp

您是否使用
try
语句尝试了
for
循环

temp = ['dcf23e','2322','acc41','4212','cdefd']
    index = 0
    for element in temp:
        try:
            element+1
        except:
            del temp[index]
        index = index+1
    print temp
或者,如果要将值转换为
int
元素,可以编写以下代码:

temp = ['dcf23e','2322','acc41','4212','cdefd']
    index = 0
    for element in temp:
        try:
            element+1
        except:
            temp[index] = 0
        index = index+1
    print temp

我读了csv链接,它实际上没有说任何关于排序包含字符串的字符的内容。我读了csv链接,它没有说任何关于排序包含字符串的字符的内容。findall函数调用将不会产生正确的结果。请测试您的代码。findall函数调用将不会产生正确的结果。请测试你的代码。