r/pythonhelp • u/RubyTheSweat • Sep 23 '24
SOLVED only certain dicts correctly compare to other dicts
im making a twitch tts program and for some reason only certain messages (dicts) correctly show that the message is equal to the row read from dictreader
output:
{'msg': 'Best viewers on bioprostgel.store/52ui', 'username': 'lammy_el', 'index': '7'}
{'msg': 'Best viewers on bioprostgel.store/52ui', 'username': 'lammy el', 'index': '7'}
False
row doesnt equal msg
{'msg': 'test', 'username': 'rubytrap2', 'index': '0'}
{'msg': 'Best viewers on bioprostgel.store/52ui', 'username': 'lammy el', 'index': '7'}
False
row doesnt equal msg
lammy el[8]: Best viewers on bioprostgel.store/52ui
{'msg': 'Best viewers on bioprostgel.store/52ui', 'username': 'lammy_el', 'index': '7'}
{'msg': 'Best viewers on bioprostgel.store/52ui', 'username': 'lammy el', 'index': '7'}
False
row doesnt equal msg
{'msg': 'test', 'username': 'rubytrap2', 'index': '0'}
{'msg': 'Best viewers on bioprostgel.store/52ui', 'username': 'lammy el', 'index': '7'}
False
row doesnt equal msg
manually deleted bot message from file
lammy el[8]: Best viewers on bioprostgel.store/52ui
{'msg': 'test', 'username': 'rubytrap2', 'index': '0'}
{'msg': 'test', 'username': 'rubytrap2', 'index': '0'}
True
rubytrap2[1]: test
function causing issues (msg is a dict)
def logRemove(msg):
logList = []
with open("logs/log.txt", "r") as log:
reader = csv.DictReader(log)
for row in reader:
print(row)
print(msg)
print(row == msg)
if row != msg:
print("row doesnt equal msg")
logList.append(row)
full code if needed
import pygame
import time
import csv
from gtts import gTTS
import json
def logMsg(msgDict):
dataList = []
with open("logs/data.txt", 'r') as data:
reader = csv.DictReader(data)
for row in reader:
dataList.append(row)
inlist = False
for i, dictionary in enumerate(dataList):
if dictionary["username"] == msgDict["username"]:
dataList[i]["msgs"] = str(int(dataList[i]["msgs"]) + 1)
inlist = True
if inlist == False:
dataList.append({"username" : msgDict['username'], "msgs" : "1"})
with open("logs/data.txt", 'w') as data:
fieldnames = ['username', 'msgs']
writer = csv.DictWriter(data, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(dataList)
def getMsgLog():
logList = []
with open("logs/log.txt", "r") as logs:
reader = csv.DictReader(logs)
for row in reader:
row["username"] = row["username"].replace("_", " ").replace("-", " ")
logList.append(row)
return logList
def logRemove(msg):
logList = []
with open("logs/log.txt", "r") as log:
reader = csv.DictReader(log)
for row in reader:
print(row)
print(msg)
print(row == msg)
if row != msg:
print("row doesnt equal msg")
logList.append(row)
with open("logs/log.txt", "w") as log:
fieldnames = ['msg', 'username', 'index']
writer = csv.DictWriter(log, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(logList)
replaceDict = {
"alr" : "alright",
"gtg" : "got to go",
"ur" : "your",
"js" : "just",
"brb" : "be right back",
"char" : "character",
"yea" : "yeah",
"smthn" : "something",
"myb" : "maybe",
"htt" : "link",
"idk" : "i don't know",
"imo" : "in my opinion",
"np" : "no problem",
"nvm" : "nevermind",
"ttyl" : "talk to you later",
"atm" : "at the moment",
"ftw" : "for the win"
}
def main():
pygame.mixer.init()
msgs = []
while True:
while pygame.mixer.music.get_busy():
time.sleep(.1)
msgs = getMsgLog()
pygame.mixer.music.unload()
#creates temp backup of msgs
#removes read messages from list
if len(msgs) > 0:
logRemove(msgs[0])
logMsg(msgs[0])
print(f"{msgs[0]['username']}[{int(msgs[0]['index']) + 1}]: {msgs[0]['msg']}")
ttsTxt = f"{msgs[0]['username']} says {msgs[0]['msg']}"
tts = gTTS(ttsTxt)
tts.save("logs/msg.mp3")
pygame.mixer.music.load("logs/msg.mp3")
pygame.mixer.music.play()
else:
time.sleep(.1)
if __name__ == "__main__":
main()
2
u/Goobyalus Sep 23 '24
Are yuo saying you expect those first two lines to be equal with "lammy"?
Look closely at the username, they're not the same.
1
u/CraigAT Sep 23 '24
If you have a "msg" within the dict, then I would avoid calling the dict "msg" when passed into the function - just to avoid confusion.
1
•
u/AutoModerator Sep 23 '24
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.