提问人:AnGeL 提问时间:3/16/2023 更新时间:3/16/2023 访问量:49
将列表与 txt 数据进行比较 (python)
Compare a list with a txt data (python)
问:
我有一个包含德国种族主义词的列表,现在我有来自德国政党 (AFD) 的推文文本,我需要进行比较。所以要得到问题的答案:用了很多种族主义的词。 但我不知道怎么做。 我知道如何比较字符串,但我无助于比较 txt 和 csv 数据或 csv 数据和 csv 数据。
那是我的程序。我的种族主义词列表由记事本制作为 csv。
import pandas as pd
import tweepy
# Importing the libraries
import configparser
import fileinput
from sklearn.model_selection import train_test_split
config = configparser.ConfigParser()
config.read('config.ini')
api_key = config['twitter']['api_key']
api_key_secret = config['twitter']['api_key_secret']
access_token = config['twitter']['access_token']
access_token_secret = config['twitter']['access_token_secret']
import tweepy
# Authenticate
auth = tweepy.OAuthHandler(api_key, api_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
user = 'AfD'
limit=100
tweets = tweepy.Cursor(api.user_timeline, screen_name=user, tweet_mode='extended').items(limit)
columns = ['Tweet']
data = []
for tweet in tweets:
data.append(tweet.full_text)
df = pd.DataFrame(data, columns=columns)
print(df)
user = 'AfD'
limit=10
page_no = 1
pages = tweepy.Cursor(api.user_timeline, screen_name=user, tweet_mode='extended').pages(limit)
for page in pages:
for i in range(len(page)):
print(f"{i+1} {page[i].full_text} \n")
print(f"*********End of Page {page_no}*********")
page_no += 1
df.to_csv('afd.csv')
data_file= pd.read_csv('afd.csv')
data_file['Tweet'] = data_file['Tweet'].apply(lambda x: " ".join(x.lower () for x in x.split()))
print(data_file)
data_file.to_csv('newAfd.csv')
我尝试过类似的东西,但我失败了
output = []
with open('Rassismus.csv') as f:
matches = [i.strip() for i in f]
with open('newAfd.txt') as f:
for i in f:
match = i.split(',')[-1].strip()
if any(match == j for j in matches):
output.append(i)
答: 暂无答案
评论
in
if any(match in i for match in matches)