0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python で環境変数で設定した値をリストで受け取ってみました

Posted at

概要

環境変数に区切り文字「,」を含んだ文字列を定義しておき、プログラム側で環境変数を取得後に、その区切り文字を利用してリスト化を行いました。

実行環境

macOS Ventura 13.0
python 3.8.12


環境変数の定義

事前に環境変数「TEST_ENV」に以下を登録しておきます。区切り文字「,」を含んだ文字列を定義しておきます。

## 登録
$ export TEST_ENV=2023-01,2023-02,2023-03

## 確認
$ echo $TEST_ENV
2023-01,2023-02,2023-03

環境変数を取得するプログラム

上記で設定した環境変数を取得する pythonプログラムは以下となります。プログラム側で環境変数を取得後に、その区切り文字「,」を利用してリスト化を行います。

split_env.py
import os

env_list = os.getenv('TEST_ENV').split(',')
print(f"env_list:{env_list}")
print('ListItem is ', len(env_list))

for env in env_list:
    print(env)

プログラムの実行

作成したプログラムから環境変数をリスト取得してみます。

$ python split_env.py
env_list:['2023-01', '2023-02', '2023-03']
ListItem is  3
2023-01
2023-02
2023-03

まとめ

これで1つの環境変数に区切り文字「,」を含んだ文字列を定義しておき、プログラム側でその環境変数を取得後に、リスト化できることを確認できました。 Azure Functions や AWS lambda の引数として利用することを想定しています。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?