首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从string python中删除时间戳和url

从string python中删除时间戳和url
EN

Stack Overflow用户
提问于 2018-08-22 04:36:17
回答 1查看 531关注 0票数 0

我有一个字符串,我必须删除其中的时间戳和标点符号。我还必须去掉所有的数字,但responseCode的值必须保持不变,例如本例中的400。无论400来自哪里,它都不应该被移除。并且我想删除所有以tar.gz结尾的url和文件名。

代码语言:javascript
复制
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]
"

预期结果:

代码语言:javascript
复制
"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
"

我的解决方案是删除标点符号:

代码语言:javascript
复制
punctuations = '''!=()-[]{};:'"\,<>.?@#$%^&*_~'''
no_punct = ""
for char in mystr:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-22 06:02:52

也许:

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51956359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档