The code im trying to create is supposed to have two keys:
["democrats"] ["republicans"]
I've nearly got it with all the correct data, I just don't have the key dictionaries. This is what I'm currently working with:
def getPartyUserTwitterUsage(tweetFile):
import csv
myFile = open(tweetFile,"r") # opening file in read
csvReader = csv.reader(myFile,delimiter=",") # splitting for ','
next(csvReader) # skipping header
tweetList = {}
repTweet = 0
demoTweet = 0
for row in csvReader: # iterating through file
if (row[1] == 'R'):
if (row[0] not in tweetList):
tweetList[row[0]] = 1
else:
tweetList[row[0]] = 1
if (row[1] == 'D'):
if (row[0] not in tweetList):
tweetList[row[0]] = 1
else:
tweetList[row[0]] = 1
return tweetList
This function: getPartyUserTwitterUsage("Tweets-2020 (2).csv")
returns:
{'ChrisMurphyCT': 1000,
'SenBlumenthal': 1000,
'SenatorCarper': 1000,
'ChrisCoons': 1000,
'brianschatz': 1000,
'maziehirono': 1000,
'SenatorDurbin': 1000,
'SenatorHarkin': 1000,
'lisamurkowski': 1000,
'JeffFlake': 1000,
'marcorubio': 1000,
'MikeCrapo': 958,
'SenatorRisch': 1000,
'ChuckGrassley': 1000,
'SenPatRoberts': 1000,
'JerryMoran': 1000}
This is the output im expecting:
{'Republicans': {'lisamurkowski': 1000,
'JeffFlake': 1000,
'marcorubio': 1000,
'MikeCrapo': 958,
'SenatorRisch': 1000,
'ChuckGrassley': 1000,
'SenPatRoberts': 1000,
'JerryMoran': 1000},
'Democrats': {'ChrisMurphyCT': 1000,
'SenBlumenthal': 1000,
'SenatorCarper': 1000,
'ChrisCoons': 1000,
'brianschatz': 1000,
'maziehirono': 1000,
'SenatorDurbin': 1000,
'SenatorHarkin': 1000}}
CodePudding user response:
You need to create the keys: Republicans
and Democrats
before inserting the entries.
...
tweetList["Republicans"] = {}
tweetList["Democrats"] = {}
for row in csvReader: # iterating through file
if (row[1] == 'R'):
if (row[0] not in tweetList["Republicans"]):
tweetList["Republicans"][row[0]] = 1
else:
tweetList["Republicans"][row[0]] = 1
if (row[1] == 'D'):
if (row[0] not in tweetList["Democrats"]):
tweetList["Democrats"][row[0]] = 1
else:
tweetList["Democrats"][row[0]] = 1
return tweetList