Utilizing Python, you may import data from fairly a number of file codecs, equal to CSV, Excel, Textual content material materials, JSON, and SQL. As rapidly as imported, you may manipulate the information utilizing libraries like pandas after which export the information to codecs like CSV, HTML, JSON, and SQL. Beneath are the detailed steps and code examples for every import and export operation.
1. Import CSV
To import a CSV file, it is best to take advantage 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 is best to take advantage 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 materials
To import a textual content material materials file, it is best to take advantage of the pandas
library. If the textual content material materials file is delimited (e.g., tab-delimited), specify the delimiter.
import pandas as pd# Import Textual content material materials 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 is best to take advantage 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 data from a SQL database, it is best to take advantage of the pandas
library together 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 data 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 data to a CSV file, it is best to take advantage 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 data to an HTML file, it is best to take advantage 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 data to a JSON file, it is best to take advantage 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 data to a SQL database, it is best to take advantage of the pandas
library together 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 data ensures you would possibly want clear steps for importing and exporting data utilizing Python utilizing fairly a number of file codecs, with every export operation following its corresponding import operation for elevated readability and group.