Home > Software engineering >  How to Write a Nested For Loop in One Line Python with File Url and File Name
How to Write a Nested For Loop in One Line Python with File Url and File Name

Time:10-21

 data = json.dumps({'conf':{a:b for a in table_name for b in file_url}})
 print(data)

I have 2 files in file_url.

sales_dy_20221022.csv
sales_wk_20221022

I have 2 table names in table_name

sales_dy
sales_wk

If I write the code like the above I get the result below.

"conf": {
"sales_dy": "sales_dy_20221022.csv",
"sales_wk": "sales_dy_20221022.csv"
}

How can I change the code and get the result below?

"conf": { "sales_dy": "sales_dy_20221022.csv", "sales_wk": "sales_wk_20221022.csv" }

CodePudding user response:

You are in the right path, use zip. ex. data = json.dumps({'conf':{a:b for a,b in zip(table_name,file_url)}})

  • Related