Hello im a beginner to python, I'm iterating through a csv file and trying to find out how many times a specific user uses the symbols: "@" and "#" and printing it out as a dictionary. The issue is I think its counting something like "@yomomma" as a non factor since the symbol isn't sperate from the word, but im also not sure.
def getUserTweetDetails(tweetFile,twitterUsername):
import csv
myFile = open(tweetFile,"r") # opening file in read
csvReader = csv.reader(myFile,delimiter=",") # splitting for ','
next(csvReader) # skipping header
userDetails = {}
mentionsCounter = 0
hashtagCounter = 0
for row in csvReader:
if (row[0] == twitterUsername):
if (row[2] == '@'):
mentionsCounter = 1
if (row[2] == '#'):
mentionsCounter = 1
userDetails["mentions"] = mentionsCounter
userDetails["hashtags"] = hashtagCounter
print(userDetails)
This returns
getUserTweetDetails("Tweets-2020 (2).csv",'ChrisMurphyCT')
**{'mentions': 0, 'hashtags': 0}**
The csv files format looks like this: twitterUsername,politicalParty,tweetText
CodePudding user response:
Your statement row[2] == '@'
checks if the tweet is equal to @
. What you want to do is see if a tweet contains this symbol with '@' in row[2]
.
Also there is a bug where you're adding to mentionsCounter
both times instead of hashtagCounter
for the second one.
And lastly a general note, I'd import csv at the top of the file and not within the function so the import is only called when the script is first run and not every time the function is called.
Here's the first two changes:
if '@' in row[2]:
mentionsCounter = 1
if '#' in row[2]:
hashtagCounter = 1 # changed from mentionsCounter to hashtagCounter
Keep in mind that if an @
or #
appear multiple times in a tweet they will only be counter once according to your code.
CodePudding user response:
Let me simplify for you;
import csv
converted_csv = csv.DictReader(open('tweets.csv'))
for row in converted_csv:
hastags = row['tweetText'].count('#')
mentions = row['tweetText'].count('@')
print(hastags, mentions)