-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconvertcsv.py
More file actions
31 lines (25 loc) · 977 Bytes
/
convertcsv.py
File metadata and controls
31 lines (25 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import sqlite3
import pandas as pd
try:
# Read the CSV file into a pandas DataFrame
df = pd.read_csv('unfriendtech-2023-08-21.csv')
print("CSV file successfully read.")
try:
# Create an SQLite database and save the DataFrame to it
conn = sqlite3.connect('mydatabase.db')
# Try writing data to the database
df.to_sql('data', conn, if_exists='replace', index=False)
print("Data successfully written to the SQLite database.")
except sqlite3.Error as e:
print(f"An SQLite error occurred: {e}")
finally:
# Close the connection (important!)
conn.close()
except FileNotFoundError:
print("Error: CSV file not found.")
except pd.errors.EmptyDataError:
print("Error: The CSV file is empty.")
except pd.errors.ParserError:
print("Error: Problem occurred during parsing the CSV.")
except Exception as e:
print(f"An unexpected error occurred: {e}")