Exporting Multiple CSV Files with Different Filenames in Python
Introduction
As a data enthusiast, working with historical stock data can be an exciting yet challenging task. In this article, we will explore how to export multiple CSV files using different filenames in Python.
Background
In the world of finance and data analysis, CSV (Comma Separated Values) files are a popular format for storing and sharing data. When dealing with large datasets or specific stock names, manually exporting each file can be time-consuming and prone to errors.
Python’s investpy library provides an easy-to-use interface for retrieving historical stock data from various sources. In this article, we will utilize this library to extract data for multiple stocks, generate separate CSV files, and export them with custom filenames.
Setting Up the Environment
Before we dive into the code, ensure you have Python installed on your system, along with the required libraries:
pip install investpy pandas
Creating a List of Stock Names
To start, create a list containing the stock names you want to extract data for. In this example, we will use two stocks: JFC and AAPL.
stocks_list = ['JFC', 'AAPL']
Exporting Data for Each Stock
Next, iterate through the stocks_list using a for loop. For each stock name, retrieve the historical data using investpy.get_stock_historical_data(). Then, generate a custom filename by concatenating ’extracted_’ with the stock name and appending ‘.csv’.
import investpy
for stock in stocks_list:
df = investpy.get_stock_historical_data(stock=stock,
country='philippines',
from_date='25/11/2020',
to_date='18/12/2020')
# Generate a custom filename for the CSV file
file_name = 'extracted_' + stock + '.csv'
# Export the data to a CSV file with index=False to exclude row indexes
df.to_csv(file_name, index=False)
Understanding the Code
Let’s break down the code snippet above:
- We iterate through each
stockname in thestocks_list. - For each stock, we use
investpy.get_stock_historical_data()to retrieve its historical data. - We create a custom filename by concatenating ’extracted_’ with the
stockname and appending ‘.csv’. - Finally, we export the retrieved data to a CSV file using
df.to_csv(), excluding row indexes by settingindex=False.
Handling Large Lists of Stock Names
When dealing with a large number of stock names (like 300+), manually typing each name can be tedious and error-prone. To simplify this process, consider the following approaches:
- Read from a CSV file: Store your list of stock names in a separate CSV file or Excel spreadsheet. Then, read this file using Python’s
csvmodule to populate yourstocks_list. - Use a database: If you have access to financial databases, such as Yahoo Finance or Alpha Vantage, consider storing your list of stock names in their respective APIs or databases.
- Script Generation: Create a script that automatically generates the list of stocks from a predefined source.
Handling Errors and Exceptions
When working with external APIs like investpy, errors can occur. To handle these situations:
- Try-Except Block: Wrap your code in a try-except block to catch any exceptions that may occur during data retrieval or file export.
- Retrying Requests: Use Python’s built-in
requestslibrary and its ability to retry failed requests with exponential backoff.
Full Code Example
Here is the full code example including error handling:
import investpy
import pandas as pd
import csv
from pathlib import Path
# Define a list of stock names (can be read from a CSV file or database)
stocks_list = ['JFC', 'AAPL']
def get_historical_data(stock):
try:
# Retrieve historical data for the given stock
df = investpy.get_stock_historical_data(
stock=stock,
country='philippines',
from_date='25/11/2020',
to_date='18/12/2020'
)
return df
except investpy.exceptions.InvestPyException as e:
print(f"An error occurred while retrieving data for {stock}: {e}")
return None
def export_data(df, stock):
try:
# Generate a custom filename for the CSV file
file_name = 'extracted_' + stock + '.csv'
# Export the data to a CSV file with index=False to exclude row indexes
df.to_csv(file_name, index=False)
except Exception as e:
print(f"An error occurred while exporting data for {stock}: {e}")
def main():
# Iterate through each stock name in the list
for stock in stocks_list:
# Retrieve historical data for the current stock
df = get_historical_data(stock)
if df is not None:
# Export the retrieved data to a CSV file
export_data(df, stock)
if __name__ == "__main__":
main()
Best Practices and Future Directions
When working with large datasets or multiple stock names, consider the following best practices:
- Use Pandas: Leverage Python’s
pandaslibrary for efficient data manipulation and analysis. - Optimize API Requests: Minimize unnecessary API requests by caching results or using batch processing techniques.
For future directions, you can explore more advanced topics such as:
- Parallel Processing: Utilize libraries like
joblibordaskto parallelize the export process for improved performance. - Filesystem Organization: Organize your CSV files in a structured manner using directory hierarchies and naming conventions.
Last modified on 2024-09-27