thanks for the follow :)
hii... if u want to make a new friend just add me on facebook! :) xx
Just wanna say if you ever feel lonely or sad or bored, just come and talk to me. I'm free anytime :)
I hope she not a spy for someone. I hope she real on neautral side. Because just her who i trust. :-)
not always but sometimes maybe :)
\u201c Funny how you get what you want and pray for when you want the same thing God wants. :)
Thank you :) can you follow me on Twitter so I can DM you?
RT dj got us a fallin in love and yeah earth number one m\u00fcsic listen thank you king :-)
found a cheeky weekend for \u00a380 return that's flights + hotel.. middle of april, im still looking pal :)
RT happy birthday mary ! Hope you have a good day :)
Thank god twitters not blocked on the school computers cause all my data is gone on my phone :(
enjoy tmrro. saw them earlier this wk here in tokyo :)
更新
Oki,也许我的问题是错的。我必须这样做:打开文件并从中读取
从中删除一些链接,名称和内容(我使用了正则表达式,但不知道它是否是正确的方法
在我得到干净的代码(只有带有悲伤的脸或快乐的脸的推文)后,我必须打印每一行,因为我必须像这样循环:
推文中的行: 如果''在线: cl.train(线, '高兴') 否则如果''在线: cl.train(线, '悲伤')
我的代码到目前为止你看到了这里,但它还没有用。
import re
from pprint import pprint
tweets = []
tweets = open('englishtweet.txt').read()
regex_username = '@[^\s]*' # regex to detect username in file
regex_url = 'http[^\s]*' # regex to detect url in file
regex_names = '#[^\s]*' # regex to detect # in file
for username in re.findall(regex_username, tweets):
tweets = tweets.replace(username, '')
for url in re.findall(regex_url, tweets):
tweets = tweets.replace(url, '')
for names in re.findall(regex_names, tweets):
tweets = tweets.replace(names, '')
答案 0 :(得分:1)
如果您想阅读第一行,请使用next
with open("englishtweet.txt","r") as infile:
print next(infile).strip()
# this prints the first line only, and consumes the first value from the
# generator so this:
for line in infile:
print line.strip()
# will print every line BUT the first (since the first has been consumed)
我也在这里使用一个上下文管理器,它会在你退出with
块后自动关闭文件,而不必记得调用tweets.close()
,并且在出现错误时也会处理(取决于您在文件中执行的其他操作,您可能会抛出一个不允许您访问.close
语句的已处理异常)。
如果您的文件非常小,可以使用.readlines
:
with open("englishtweet.txt","r") as infile:
tweets = infile.readlines()
# tweets is now a list, each element is a separate line from the file
print tweets[0] # so element 0 is the first line
for line in tweets[1:]: # the rest of the lines:
print line.strip()
然而,并没有真正建议将整个文件对象读入内存,就像某些文件一样,它只是一个巨大的内存浪费,特别是如果你只需要第一行 - 没有理由将整个内容读入内存。
那就是说,因为看起来你可能不仅仅使用这些迭代一次,也许readlines
IS 是最好的方法
答案 1 :(得分:0)
你几乎拥有它。最初打开文件时只需删除.read()。然后你可以遍历这些线。
tweets = open('englishtweet.txt','r')
for line in tweets:
print line
tweets.close()