Python 如何从推文中删除日期?
需要一些建议…我收集了一些推特Python 如何从推文中删除日期?,python,regex,Python,Regex,需要一些建议…我收集了一些推特 Mon Apr 06 22:19:45 PDT @switchfoot http://twitpic.com/2y1zl - Awww, that's a bummer. :( You shoulda got David Carr of Third Day to do it. ;D Mon Apr 06 22:19:49 PDT is upset that he can't update his Facebook by texting it... and migh
Mon Apr 06 22:19:45 PDT @switchfoot http://twitpic.com/2y1zl - Awww, that's a bummer. :( You shoulda got David Carr of Third Day to do it. ;D
Mon Apr 06 22:19:49 PDT is upset that he can't update his Facebook by texting it... and might cry as a result :( School today also. Blah!
Mon Apr 06 22:19:53 PDT @Kenichan I dived many times for the ball. Managed to save 50% :( The rest go out of bounds
Mon Apr 06 22:19:57 PDT my whole body feels itchy and like its on fire :(
我如何删除此周一4月06日22:19:57 PDT?使用regex?如果这是一个字符串,只需在第一个
PDT
上拆分该行即可:
for line in tweets.splitlines():
print line.split(' PDT ', 1)[1]
第一次出现字符PDT
(带空格)时,该行被拆分,并打印结果的后半部分
但是,也许您可以首先阻止输出字符串的代码添加日期
for line in lines:
print line[24:]
如果日期/时间格式始终相同,则可能很简单。如果它们都是字符串,则以相同的方式存储,您只需执行拆分:
tweet = "Mon Apr 06 22:19:57 PDT SomeGuy Im not white enough to be excited for a new version of Windows".
tweet= tweet.split(None, 5)[-1]
结果推特被删除
“有人说我不够白,不能为新版本的
窗口“
似乎把它分成一个单词列表,去掉前六个更可能在时区变化中保持一致
clean_tweets = []
for tweet in tweets:
words = tweet.split()
del words[0:5]
clean_tweet = " ".join(words)
clean_tweets.append(clean_tweet)
默认情况下,将在空格上拆分,因此不必指定分隔符 我假定您不能使用PDT,因为您不能假定它们始终是PDT。似乎字符串中最容易识别的部分是[0-9]+:[0-9]+:[0-9]+-时间
/^.*[0-9]+:[0-9]+:[0-9]+\s+[A-Z]{3}\s*(.*)$/
捕获所有大写字母中时间和3个字母时区后的字符串。是什么生成该输出的?你不能改变吗?我试过regex re.sub,但结果不满意…对Martijn来说,输出的是相同的推文,但没有日期,这个PDT的东西…实际上所有的推文都是一样的…与PDT有相同的日期形式…PDT到底意味着什么?@Katrielex:当然,但是根本没有关于时间戳中有什么变化的信息。哦,真的,真的。但是我认为,假设时间总是被格式化比假设它们总是在同一时区更安全。Thx这对我来说真的很有效。)我是新手,没有进去。你能解释一下为什么你使用拆分吗?@Aikin:使用固定格式,总是有一组容易识别的字符划界时间部分,
.split()
是最简单、最容易理解的方法。除非你能改变tweet格式,否则我认为这是最好的方法——它只依赖于twitter输出格式,而不改变。更简洁。我不知道斯普利特能做到。现在我知道了。文档: