Python 我正在替换tweets csv文件中的一些单词,但是tweets显示为空字符串

Python 我正在替换tweets csv文件中的一些单词,但是tweets显示为空字符串,python,regex,csv,lemmatization,Python,Regex,Csv,Lemmatization,我有一个引理和同义词的数据集,另一方面我有tweets数据集,我试图用适当的引理替换一些单词。 问题是,当我运行脚本时,第一条tweet会根据需要处理,但其他tweet会出现空字符串 以下是同义词csv文件的外观: lemma,part_of_speech,synonyms jakob-creutzfeldt disease,noun,Creutzfeldt-Jakob disease;CJD;Jakob-Creutzfeldt disease jakob behmen,noun,Boehme;

我有一个引理和同义词的数据集,另一方面我有tweets数据集,我试图用适当的引理替换一些单词。 问题是,当我运行脚本时,第一条tweet会根据需要处理,但其他tweet会出现空字符串

以下是同义词csv文件的外观:

lemma,part_of_speech,synonyms
jakob-creutzfeldt disease,noun,Creutzfeldt-Jakob disease;CJD;Jakob-Creutzfeldt disease
jakob behmen,noun,Boehme;Jakob Boehme;Bohme;Jakob Bohme;Boehm;Jakob Boehm;Behmen;Jakob Behmen
jakob bernoulli,noun,Bernoulli;Jakob Bernoulli;Jacques Bernoulli;James Bernoulli
jakob boehm,noun,Boehme;Jakob Boehme;Bohme;Jakob Bohme;Boehm;Jakob Boehm;Behmen;Jakob Behmen
jakob boehme,noun,Boehme;Jakob Boehme;Bohme;Jakob Bohme;Boehm;Jakob Boehm;Behmen;Jakob Behmen
jakob bohme,noun,Boehme;Jakob Boehme;Bohme;Jakob Bohme;Boehm;Jakob Boehm;Behmen;Jakob Behmen
jakob grimm,noun,Grimm;Jakob Grimm;Jakob Ludwig Karl Grimm
jakob hermandszoon,noun,Arminius;Jacobus Arminius;Jacob Harmensen;Jakob Hermandszoon
jakob liebmann beer,noun,Meyerbeer;Giacomo Meyerbeer;Jakob Liebmann Beer
以下是数据集的外观:

Tweets
english translation: 'a message to the truthful in syria - sheikh abu muhammed al maqdisi: https://justpaste.it/messagetothetruthfulinsyria https://twitter.com/account/suspended
"english translation: sheikh fatih al jawlani 'for the people of integrity sacrifice is easy' https://justpaste.it/sheikhaljawlaninewmessage https://twitter.com/account/suspended "
english translation: first audio meeting with sheikh fatih al jawlani (ha): https://justpaste.it/meetingwithsheikhfatih https://twitter.com/account/suspended
"english translation: sheikh nasir al wuhayshi (ha) leader of aqap: 'the promise of victory': https://justpaste.it/aqappromiseofvictory https://twitter.com/account/suspended "
english translation: aqap: 'response to sheikh baghdadis statement 'although the disbelievers dislike it.' https://justpaste.it/i4xj
the second clip in a da'wah series by a soldier of jn: video link :https://www.youtube.com/watch?v=ugjnkygfxr4%26feature=youtu.be https://twitter.com/account/suspended
english transcript : oh murabit! : https://justpaste.it/ohmurabit https://twitter.com/account/suspended
english translation: 'a collection of the words of the u'lama regarding dawlah': https://justpaste.it/ulamaregardingdawlah https://twitter.com/account/suspended
aslm please share our new account after the previous one was suspended. @khalidmaghrebi @seifulmaslul123 @cheerleadunited
english translation: aqap statement regarding the blessed raid in france:https://justpaste.it/aqapfrenchraid https://twitter.com/account/suspended
@khalidmaghrebi @seifulmaslul123 @cheerleadunited @ibnnabih1
我的代码:

import csv
import re

with open("DataSet_ISIS20000_new.csv", 'w') as new_csvData:
    new_dataset = csv.DictWriter(new_csvData, fieldnames=["Tweets"])
    new_dataset.writeheader()
    
    with open("testing_data.csv", 'r') as csvData:
        dataset = csv.DictReader(csvData, fieldnames=["Tweets"])

        with open("new_synonyms.csv", 'r') as csvSynonyms:
            synonym_rows = csv.DictReader(csvSynonyms, fieldnames=['lemma', 'part_of_speech', 'synonyms'], delimiter=',')

            next(dataset, None)
            for tweet in dataset:
                new_tweet = {}
                for row in synonym_rows:
                    synList = row['synonyms'].split(';')
                    for word in synList:
                        new_tweet['Tweets'] = re.sub(word, row['lemma'], tweet['Tweets'])

                new_dataset.writerow(new_tweet)


print("************************* done ! ***************************")                    
                
以下是输出(使用六个TWWET进行测试):


现在是学习和逐步完成代码并观察每行代码的功能的好时机。通过将这些中间结果与预期结果进行比较,确定您的计划与您的预期不同之处。从那里向后努力,缩小问题的原因。在您的例子中,您会注意到,
synonym_rows
迭代器在第一次迭代@PranavHosangadi之后已经耗尽,我完全忘记了这一点!现在我明白了,非常感谢你。。。我会照你的建议做的再次谢谢
Tweets
english translation: 'a message to the truthful in syria - sheikh abu muhammed al maqdisi: https://justpaste.it/messagetothetruthfulinsyria https://twitter.com/account/suspended
""
""
""
""
""