String 从字符串python中删除时间戳和url
我有一个字符串,我必须从中删除时间戳和标点符号。我必须删除所有的数字,除了responseCode值 在这种情况下,必须保持原样,例如400。无论400人到哪里,都不应将其移除。我想删除所有的url 文件名以tar.gz结尾String 从字符串python中删除时间戳和url,string,python-3.x,timestamp,data-cleaning,String,Python 3.x,Timestamp,Data Cleaning,我有一个字符串,我必须从中删除时间戳和标点符号。我必须删除所有的数字,除了responseCode值 在这种情况下,必须保持原样,例如400。无论400人到哪里,都不应将其移除。我想删除所有的url 文件名以tar.gz结尾 mystr="sun aug 19 13:02:09 2018 I_am.98189: hello please connect to the local host:8080 sun aug 19 13:02:10 2018 hey.94289: hello not
mystr="sun aug 19 13:02:09 2018 I_am.98189: hello please connect to the local host:8080
sun aug 19 13:02:10 2018 hey.94289: hello not able to find the file
sun aug 19 13:02:10 2018 I_am.94289: Base url for file_transfer is: abc/vd/filename.tar.gz
mon aug 19 13:02:10 2018 how_94289: $var1={
'responseCode' = '400',
'responseDate' = 'Sun, 19 Aug 2018 13:02:08 ET',
'responseContent' = 'ABC' }
mon aug 20 13:02:10 2018 hello!94289: Error performing action, failed with error code [400]
"
预期结果:
"I_am hello please connect to the local host
hello not able to find the file
Base url for file_transfer
var1
responseCode = 400
responseDate
responseContent = ABC
Error performing action, failed with error code 400
"
我删除标点符号的解决方案:
punctuations = '''!=()-[]{};:'"\,<>.?@#$%^&*_~'''
no_punct = ""
for char in mystr:
if char not in punctuations:
no_punct = no_punct + char
# display the unpunctuated string
print(no_punct)
标点符号=“”=()-[]{};:'"\,.?@#$%^&*_~'''
否_punt=“”
对于mystr中的char:
如果字符不在标点符号中:
无点=无点+字符
#显示未定时的字符串
打印(无点)
也许:
patterns = [r"\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2} \d{4}\s*", #sun aug 19 13:02:10 2018
r"\w{3}, \d{2} \w{3} \d{4} \d{2}:\d{2}:\d{2} \w{2}\s*", #Sun, 19 Aug 2018 13:02:08 ET
r":\s*([\da-zA_Z]+\/)+([a-zA-Z0-9\.]+)", #URL
r"([a-zA-Z_!]+)[\.!_]\d+:\s*", #word[._!]number:>=0space
r":\d+",
"[/':,${}\[\]]" #punctuations
]
s = mystr
for p in patterns:
s = re.sub(p,'', s)
s = s.strip()
print(s)
输出:
hello please connect to the local host
hello not able to find the file
Base url for file_transfer is
var1=
responseCode = 400
responseDate =
responseContent = ABC
Error performing action failed with error code 400
嗨,我更新了问题。98189不总是在我之后。你能编辑你的答案吗?