Skip to main content
 首页 » 编程设计

python之在 PYTHON 中读取行、处理列表和写入文件

2024年11月01日10bhlsheji

我是 Python 的新手,我正在处理以下推文:

@PrincessSuperC Hey Cici sweetheart! Just wanted to let u know I luv u! OH! and will the mixtape drop soon? FANTASY RIDE MAY 5TH!!!!   
@Msdebramaye I heard about that contest! Congrats girl!!  
UNC!!! NCAA Champs!! Franklin St.: I WAS THERE!! WILD AND CRAZY!!!!!! Nothing like it...EVER http://tinyurl.com/49955t3 
Do you Share More #jokes #quotes #music #photos or #news #articles on #Facebook or #Twitter? 
Good night #Twitter and #TheLegionoftheFallen.  5:45am cimes awfully early! 
I just finished a 2.66 mi run with a pace of 11'14"/mi with Nike+ GPS. #nikeplus #makeitcount 
Disappointing day. Attended a car boot sale to raise some funds for the sanctuary, made a total of 88p after the entry fee - sigh 
no more taking Irish car bombs with strange Australian women who can drink like rockstars...my head hurts. 
Just had some bloodwork done. My arm hurts 

它应该有一个特征向量的输出如下:

featureList = ['hey', 'cici', 'luv', 'mixtape', 'drop', 'soon', 'fantasy', 'ride', 'heard',  
'congrats', 'ncaa', 'franklin', 'wild', 'share', 'jokes', 'quotes', 'music', 'photos', 'news', 
'articles', 'facebook', 'twitter', 'night', 'twitter', 'thelegionofthefallen', 'cimes', 'awfully', 
'finished', 'mi', 'run', 'pace', 'gps', 'nikeplus', 'makeitcount', 'disappointing', 'day', 'attended', 
'car', 'boot', 'sale', 'raise', 'funds', 'sanctuary', 'total', 'entry', 'fee', 'sigh', 'taking', 
'irish', 'car', 'bombs', 'strange', 'australian', 'women', 'drink', 'head', 'hurts', 'bloodwork',  
'arm', 'hurts'] 

但是,我得到的当前输出只有

hey, cici, luv, mixtape, drop, soon, fantasy, ride 

仅来自第一条推文。而且它只在一条推文中循环而不会转到下一行。我尝试使用 nextLine 但显然它在 Python 上不起作用。我的代码如下:

#import regex 
import re 
import csv 
import pprint 
import nltk.classify 
 
#start replaceTwoOrMore 
def replaceTwoOrMore(s): 
    #look for 2 or more repetitions of character 
    pattern = re.compile(r"(.)\1{1,}", re.DOTALL)  
    return pattern.sub(r"\1\1", s) 
#end 
 
#start process_tweet 
def processTweet(tweet): 
    # process the tweets 
 
    #Convert to lower case 
    tweet = tweet.lower() 
    #Convert www.* or https?://* to URL 
    tweet = re.sub('((www\.[\s]+)|(https?://[^\s]+))','URL',tweet) 
    #Convert @username to AT_USER 
    tweet = re.sub('@[^\s]+','AT_USER',tweet)     
    #Remove additional white spaces 
    tweet = re.sub('[\s]+', ' ', tweet) 
    #Replace #word with word 
    tweet = re.sub(r'#([^\s]+)', r'\1', tweet) 
    #trim 
    tweet = tweet.strip('\'"') 
    return tweet 
#end  
 
#start getStopWordList 
def getStopWordList(stopWordListFileName): 
    #read the stopwords 
    stopWords = [] 
    stopWords.append('AT_USER') 
    stopWords.append('URL') 
 
    fp = open(stopWordListFileName, 'r') 
    line = fp.readline() 
    while line: 
        word = line.strip() 
        stopWords.append(word) 
        line = fp.readline() 
    fp.close() 
    return stopWords 
#end 
 
#start getfeatureVector 
#start getfeatureVector 
def getFeatureVector(tweet): 
    featureVector = [] 
    #split tweet into words 
    words = tweet.split() 
    for w in words: 
        #replace two or more with two occurrences 
        w = replaceTwoOrMore(w) 
        #strip punctuation 
        w = w.strip('\'"?,.') 
        #check if the word stats with an alphabet 
        val = re.search(r"^[a-zA-Z][a-zA-Z0-9]*$", w) 
        #ignore if it is a stop word 
        if(w in stopWords or val is None): 
            continue 
        else: 
            featureVector.append(w.lower()) 
    return featureVector 
#end 
 
#Read the tweets one by one and process it 
fp = open('data/sampleTweets.txt', 'r') 
line = fp.readline() 
 
st = open('data/feature_list/stopwords.txt', 'r') 
stopWords = getStopWordList('data/feature_list/stopwords.txt') 
 
while line: 
    processedTweet = processTweet(line) 
    featureVector = getFeatureVector(processedTweet) 
    with open('data/niek_corpus_feature_vector.txt', 'w') as f: 
        f.write(', '.join(featureVector)) 
#end loop 
fp.close() 

更新: 尝试按照以下建议更改循环后:

st = open('data/feature_list/stopwords.txt', 'r') 
stopWords = getStopWordList('data/feature_list/stopwords.txt') 
 
with open('data/sampleTweets.txt', 'r') as fp: 
    for line in fp: 
        processedTweet = processTweet(line) 
        featureVector = getFeatureVector(processedTweet) 
        with open('data/niek_corpus_feature_vector.txt', 'w') as f: 
            f.write(', '.join(featureVector)) 
fp.close() 

我得到了以下输出,这只是推文最后一行中的单词。

bloodwork, arm, hurts 

我还在想办法。

请您参考如下方法:

如果您只想使用 readline() 而不是 readlines,请使用如下循环。

st = open('data/feature_list/stopwords.txt', 'r') 
stopWords = getStopWordList('data/feature_list/stopwords.txt') 
with open('data/sampleTweets.txt', 'r') as fp: 
    for line in fp: 
        processedTweet = processTweet(line) 
        featureVector = getFeatureVector(processedTweet) 
        with open('data/niek_corpus_feature_vector.txt', 'ab') as f: 
            f.write(', '.join(featureVector))