Utilizing Python, you’ll be able to import information from numerous file codecs, equivalent to CSV, Excel, Textual content, JSON, and SQL. As soon as imported, you’ll be able to manipulate the info utilizing libraries like pandas after which export the info 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, you should utilize 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, you should utilize 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
To import a textual content file, you should utilize the pandas
library. If the textual content file is delimited (e.g., tab-delimited), specify the delimiter.
import pandas as pd# Import Textual content 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, you should utilize 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 information from a SQL database, you should utilize 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 information 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 information to a CSV file, you should utilize 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 information to an HTML file, you should utilize 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 information to a JSON file, you should utilize 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 information to a SQL database, you should utilize 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 information ensures you might have clear steps for importing and exporting information utilizing Python utilizing numerous file codecs, with every export operation following its corresponding import operation for higher readability and group.