I'm a newbie in Python, and I'm working on a project where I need to categorize tweets. I've constructed a tweet table in MySQL database with the following attributes: (tweet id,id user,text,tweet location,created at,name screen, categorie name) that will be entered by the information in each tweet that relates to them.
Error:
Code
line 43, in <module>
data['user_screen_name'],data['cat'])
KeyError: 'cat'
Data['cat'] has already been declared in another program that does categorization in the same directory.
Code
import mysql.connector
import json
# create the key
mydb = mysql.connector.connect(host='localhost', port='3306', user='root', password='nihad147', database='tweets')
mycursor = mydb.cursor()
sql_tweet = """INSERT INTO tweet ( tweet_id,
id_user,
text,
tweet_location,
created_at,
name_screen,
categorie_name,
)
VALUES (%s,%s,%s,%s,%s,%s,%s)"""
c = 0
for line in myJsonFile:
c = c + 1
print("tweet number ", c, " is uploading to the server")
data = json.loads(line)
# insert into tweet
val_tweet = (
data['tweet_id'], data['user_id_str'], data['raw_text'], data['location']['address']['city'], data['date'],
data['user_screen_name'],data['cat'])
print('nihad')
mycursor.execute(sql_tweet, val_tweet)
mydb.commit()
Is it necessary to connect the two programs or not? I've read online from various sources ([URL]https://www.scaler.com/topics/insert-in-python/[/URL]) that suggested to use from data.get("date", None) instead of data["date"] this code returns a None value if date key no exists instead of raising an error. Is that correct? Any assistance would be much appreciated.