Created
January 9, 2024 10:39
-
-
Save Hosuke/00919a2f24108f0f9867e65d7e7734a6 to your computer and use it in GitHub Desktop.
csv to trino static query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
if __name__ == '__main__': | |
# Reload the CSV file due to code execution state reset | |
file_path = '01HKPMHCEM0TFYD6KNPM66XHTT.csv' | |
df = pd.read_csv(file_path) | |
# Construct the SQL query in the desired format with dynamic field names | |
sql_query = "SELECT " | |
column_names = df.columns.tolist() | |
sql_query += ", ".join([f"{col} AS {col}" for col in column_names]) + " FROM (VALUES " | |
value_strs = [] | |
for index, row in df.iterrows(): | |
row_values = [] | |
for col in column_names: | |
value = row[col] | |
# Format the value based on its type (string or numeric) | |
if isinstance(value, str): | |
# Add '0x' prefix if it's a contract address without the prefix | |
if col == 'contract_address' and not value.startswith('0x'): | |
value = f"0x{value}" | |
row_values.append(f"'{value}'") | |
else: | |
row_values.append(str(value)) | |
value_str = f"({', '.join(row_values)})" | |
value_strs.append(value_str) | |
sql_query += ",\n ".join(value_strs) + ")" | |
# Limit the output length to avoid excessively long outputs | |
sql_query_complete = sql_query | |
sql_query_complete[:1000] # Display the first 1000 characters of the query for preview | |
# Output file path | |
output_file_path = 'trino_query.sql' | |
# Write the SQL query to a file | |
with open(output_file_path, 'w') as file: | |
file.write(sql_query) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment