Using Python, you can import info from quite a few file codecs, equal to CSV, Excel, Textual content material, JSON, and SQL. As quickly as imported, you can manipulate the data using libraries like pandas after which export the data to codecs like CSV, HTML, JSON, and SQL. Beneath are the detailed steps and code examples for each import and export operation.
1. Import CSV
To import a CSV file, it’s best to make the most of the pandas
library.
import pandas as pd# Import CSV file
df_csv = pd.read_csv('path_to_your_file.csv')
print(df_csv.head())
Documentation : https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
2. Import Excel
To import an Excel file, it’s best to make the most of the pandas
library.
import pandas as pd# Import Excel file
df_excel = pd.read_excel('path_to_your_file.xlsx', sheet_name='Sheet1')
print(df_excel.head())
Documentation: https://pandas.pydata.org/docs/reference/api/pandas.read_excel.html
3. Import Textual content material
To import a textual content material file, it’s best to make the most of the pandas
library. If the textual content material file is delimited (e.g., tab-delimited), specify the delimiter.
import pandas as pd# Import Textual content material file
df_text = pd.read_csv('path_to_your_file.txt', delimiter='t')
print(df_text.head())
4. Import JSON
To import a JSON file, it’s best to make the most of the pandas
library.
import pandas as pd# Import JSON file
df_json = pd.read_json('path_to_your_file.json')
print(df_json.head())
Documentation: https://pandas.pydata.org/docs/reference/api/pandas.read_json.html
5. Import SQL
To import info from a SQL database, it’s best to make the most of the pandas
library along with SQLAlchemy.
import pandas as pd
from sqlalchemy import create_engine# Create a SQLAlchemy engine
engine = create_engine('sqlite:///path_to_your_database.db')
# Import info from SQL
df_sql = pd.read_sql('SELECT * FROM your_table_name', con=engine)
print(df_sql.head())
Documentation: https://pandas.pydata.org/docs/reference/api/pandas.read_sql.html
1. Export to CSV
To export info to a CSV file, it’s best to make the most of the pandas
library.
# Export DataFrame to CSV
df_csv.to_csv('output_file.csv', index=False)
Documentation: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html
2. Export to HTML
To export info to an HTML file, it’s best to make the most of the pandas
library.
# Export DataFrame to HTML
df_csv.to_html('output_file.html')
Documentation: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_html.html
3. Export to JSON
To export info to a JSON file, it’s best to make the most of the pandas
library.
# Export DataFrame to JSON
df_csv.to_json('output_file.json')
Documentation: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_json.html
4. Export to SQL
To export info to a SQL database, it’s best to make the most of the pandas
library along with SQLAlchemy.
# Export DataFrame to SQL
df_csv.to_sql('your_table_name', con=engine, if_exists='change', index=False)
Documentation: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_sql.html
This info ensures you might need clear steps for importing and exporting info using Python using quite a few file codecs, with each export operation following its corresponding import operation for increased readability and group.