简体   繁体   中英

How to concatenate Python environment variable in between the strings?

I'm creating a Lambda function with python for STAGE and PROD environment which will generate a random CSV files. But I need to have a dynamic filename.

I have the current code as below: filename = "/tmp/userdata_%d%d%d-%d-%d-%d.csv" % (ts.tm_year, ts.tm_mon, ts.tm_mon, ts.tm_hour, ts.tm_min, ts.tm_sec)

I have configured ENVIRONMENT VARIABLES within the lambda function (like ENV = prod)

Expected Behavior:

  • Stage filename - staging_userdata_202211-20-4-53.csv
  • Prod filename - production_userdata_202211-23-3-35.csv

I tried something like below, but ended up with errors as I'm very new to Python.

file_prefix = "/tmp/"
file_suffix = "userdata_%d%d%d-%d-%d-%d.csv" % (ts.tm_year, ts.tm_mon, ts.tm_mon, ts.tm_hour, ts.tm_min, ts.tm_sec)
filename = (os.path.join(file_prefix, os.getenv('ENV'), file_suffix))

Got the below error:

{
  "errorMessage": "[Errno 2] No such file or directory: '/tmp/staging/userdata_202211-21-27-36.csv'",
  "errorType": "FileNotFoundError",
  "stackTrace": [
    "  File \"/var/task/generate_csv.py\", line 26, in handler\n    with open(filename, 'w', newline='') as csvfile:\n"
  ]
}

You can easily merge the environment name into the existing string formatting operation you're doing. Just add a %s where you want the environment name to show up, and put the variable you've stored it in in the right place in the tuple of values to be substituted:

env = os.getenv("ENV")
filename = "/tmp/%s_userdata_%d%d-%d-%d-%d.csv" % (env, ts.tm_year, ts.tm_mon, ts.tm_hour, ts.tm_min, ts.tm_sec)

Note, I removed a duplicated inclusion of ts.tm_mon twice in the output. It may be that you want the month to always use two digits, in which case you should replace the second %d with %02d which will zero-pad one digit month numbers. You may want that for the time numbers too!

It's not necessary, but you could take this as an opportunity to change up your string formatting style. You're using one of the oldest styles, using the % operator. That style of formatting is based on C's printf syntax. A newer method is str.format , and the newest is f-strings. Here's what an f-string version would look like:

filename = f"/tmp/{env}_userdata_{ts.tm_year}{ts.tm_mon:02d}-{ts.tmhour:02d}-{ts.tm_min:02d}-{ts.tm_sec:02d}.csv"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM