Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Required
- # python version 3.10.8
- # pip install mysql-connector-python
- # pip install googletrans==4.0.0-rc1
- import mysql.connector
- from googletrans import Translator
- # Connect to MySQL
- db = mysql.connector.connect(
- host="localhost",
- user="root",
- password="",
- database="practice_db"
- )
- cursor = db.cursor()
- translator = Translator()
- # Fetch English text
- cursor.execute("SELECT village_id, village_name FROM tbl_village WHERE village_name_guj IS NULL LIMIT 20")
- rows = cursor.fetchall()
- # Translate and update
- for row in rows:
- village_id, english_text = row
- try:
- translated_text = translator.translate(english_text, src='en', dest='gu').text
- cursor.execute("UPDATE tbl_village SET village_name_guj = %s WHERE village_id = %s", (translated_text, village_id))
- db.commit()
- except Exception as e:
- print(f"Error translating '{english_text}': {e}")
- # Close the cursor and database connection
- cursor.close()
- db.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement