在Python中进行预处理文本后,如何删除空值



例如我有一条推文" @cintya @groot @smanela https://博客..."我进行了一个预处理过程,链接和提及已被删除,我认为应该丢失。但是在CSV中,他们返回一个空值。我该如何修复它们?这是我的代码

def replaceMultiple(mainString, toBeReplaces, newString):
    for elem in toBeReplaces :
        if elem in mainString :
            mainString = mainString.replace(elem, newString)
    return  mainString
with open('datalatihNegatif.csv', encoding='utf-8') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        _word = []
        username = row[0]
        date = row[1]
        text = row[2].lower()
        text = re.sub(r'@[A-Za-z0-9_]+','',text)
        text = re.sub(r'httpS+', '',text)
        text = replaceMultiple(text, ["!","@","#","$","%","^","&","*","(",
                                      ")","_","-","+","=","{","}","[","]",
                                      "\","/",",",".","?","<",">",":",";",
                                      "'",'"',"~","0","1","2","3","4","5","6","7","8","9"], '')
        text = text.strip()
        nltk_tokens = nltk.word_tokenize(text)
        stop_words = set(stopwords.words("indonesian"))
        stop_words_new = ['i','liked','video','an','at','ba','da','do','ka','ma','ta','uh','yg','al','eh','ha','ah','ng']
        new_stopwords_list = stop_words.union(stop_words_new)
        print(username)
        print(date)
        for word in nltk_tokens:
            if word not in new_stopwords_list:
                if stemmer.stem(word) != "":
                    _word.append(stemmer.stem(word))
        print(_word)
        csvFile = open('preprocessingDLNegatif.csv', 'a', newline='')
        csvWriter = csv.writer(csvFile)
        csvWriter.writerow(_word)
        csvFile.close()

我期望CSV中的结果已删除,但实际输出是CSV中的空值1行481是空值,我如何删除它?

在写出_word中的任何内容:

if len(_word) != 0:
    csvFile = open('preprocessingDLNegatif.csv', 'a', newline='')
    csvWriter = csv.writer(csvFile)
    csvWriter.writerow(_word)
    csvFile.close()

我也不会为您编写的每个记录打开关闭输出文件。在循环之前打开一次并在完成后关闭。这样做会使我的答案看起来像:

if len(_word) != 0:
    csvWriter.writerow(_word)

最新更新