Python 如何从字符串中删除所有非字母字符?

Python 如何从字符串中删除所有非字母字符?,python,hex,hexdump,Python,Hex,Hexdump,我一直在开发一个程序,该程序将采用一个十六进制文件,如果文件名以“CID”开头,那么它应该删除前104个字符,然后是几个单词。我还想删除单词后面的所有内容,但问题是我要分离的部分长度不同 我的代码当前是这样的: y = 0 import os files = os.listdir(".") filenames = [] for names in files: if names.endswith(".uexp"): filenames.append(names)

我一直在开发一个程序,该程序将采用一个十六进制文件,如果文件名以“CID”开头,那么它应该删除前104个字符,然后是几个单词。我还想删除单词后面的所有内容,但问题是我要分离的部分长度不同

我的代码当前是这样的:

y = 0
import os
files = os.listdir(".")

filenames = []
for names in files:
    if names.endswith(".uexp"):
        filenames.append(names)
        y +=1
        print(y)
print(filenames)

for x in range(1,y):
    filenamestart = (filenames[x][0:3])
    print(filenamestart)
    if filenamestart == "CID":
        openFile = open(filenames[x],'r')
        fileContents = (openFile.read())
        ItemName = (fileContents[104:])
        print(ItemName)
输入示例文件(从HxD中提取):

我已经开始删除前104个字符,但我也希望删除“Sun Tan Specialist”之后的字符,这将在长度上有所不同,因此我只剩下这一部分


非常感谢任何人能给我的帮助。

删除字符串中非字母字符的一种方法是使用正则表达式[]

编辑

第一个参数
r'[^a-z]'
是捕获要删除内容的模式(此处,将其替换为空字符串
'
)。方括号用于表示类别(模式将匹配此类别中的任何内容),
^
是一个“not”运算符,
a-z
表示所有小写字母V字符。更多信息请点击此处:

例如,保留大写字母和空格应该是:

>>> re.sub(r'[^a-zA-Z ]', '', 'Lol !this *is* a3 -test\t12378')
'Lol this is a test'

然而,从您在问题中提供的数据来看,您需要的确切过程似乎比“去除非字母字符”要复杂一些。

您在一篇评论中提到,您将字符串归结为
Sun Tan SpecialisteffbfectdoutfittfBeceafeafadont Burning

基本上,您现在的目标是删除没有紧跟在小写字母后面的任何大写字母,因为上下表示短语的开头。您可以使用for循环来执行此操作

import re

h =  "Sun Tan SpecialistFEFFBFFECDOutfitDFBECFECAFEAFADont get burned"

output = ""
for i in range(0, len(h)):
    # Keep spaces
    if h[i] is " ":
        output += h[i]
    # Start of a phrase found, so separate with space and store character
    elif h[i].isupper() and h[i+1].islower():
        output += " " + h[i]
    # We want all lowercase characters
    elif h[i].islower():
        output += h[i]

# [1:] because we appended a space to the start of every word
 print output[1:]
 # If you dont care about Outfit since it is always there, remove it
 print output[1:].replace("Outfit", "")
输出:

晒黑专家装不会被烫伤


晒黑专家不要烫伤

您可以使用
过滤器

导入字符串
打印(“”.join(过滤器(lambda字符:string.ascii_字母+string.digits,”(ABC),DEF!))#=>ABCDEF

您的问题标题是“从字符串中删除非字母字符”。从你问题的内容来看,似乎还有更多的要求。请用示例输入、所需输出和示例文件说明我添加了一个示例文件,我希望输出是什么?您所需的输出是什么?只要
Sun Tan Specialist
?尝试类似
re.sub(r'(.*)(\..*),r'\1',s[104:])
在这一点上是的,但理想情况下我想:“Sun Tan Specialist |不要被烧伤”这可能是一种可能性,但我也希望保留大写字母和空格,我该如何做到这一点?
>>> re.sub(r'[^a-zA-Z ]', '', 'Lol !this *is* a3 -test\t12378')
'Lol this is a test'
import re

h =  "Sun Tan SpecialistFEFFBFFECDOutfitDFBECFECAFEAFADont get burned"

output = ""
for i in range(0, len(h)):
    # Keep spaces
    if h[i] is " ":
        output += h[i]
    # Start of a phrase found, so separate with space and store character
    elif h[i].isupper() and h[i+1].islower():
        output += " " + h[i]
    # We want all lowercase characters
    elif h[i].islower():
        output += h[i]

# [1:] because we appended a space to the start of every word
 print output[1:]
 # If you dont care about Outfit since it is always there, remove it
 print output[1:].replace("Outfit", "")