无法将字符串转换为变量以进行dict值分配?

发布于 2025-01-29 03:37:24 字数 1941 浏览 2 评论 0原文

我目前正在撰写Lambda功能(Python 3.9),目的是寻找在各自国家庆祝假期的学生。首先,我有一个学生。json文件和一个文件夹,其中包含holday_folder在我的S3桶中的所有国家假期:

    (main directory)
    student.json
    holiday_folder
     (within holiday_folder)
     holiday_us.json
     holiday_cn.json
     holiday_my.json
     holiday_th.json
     the list goes on...

student.json文件具有以下内容内容:

{'student': [{'name':'Adam', 'location':'US'}, {'name':'Ben', 'location':'SG'}...]}

虽然所有节日json文件的结构类似于以下内容:

{ "holiday_us":[ { "date":"20220101", "desc":"New Year's Day" }, {"date"....}]}

要实现我的目标,i:

  1. json.loads student.json content to variable Student> Student P>

  2. 创建一个名为code> nistry的列表,并运行A用于循环以仅附加“位置” “ 学生中所有学生的价值

  3. 创建另一个名为holday_calendar student变量的“位置”值/code>要存储转换的位置值aka转换['us',sg',...]到['holday_us','holday_sg'...],以便我可以将这些列表值转换为可变名称以加载假期日期

  4. student上运行有条件的循环条件。循环将查看仅与特定学生相关的假日列表,并相应地返回价值。我不会对此深入研究,因为这不是问题的重点。

     国家
     holiday_calendar = []
     对于我的范围(Len(student)):
         如果学生[i] ['位置']不在国家 /地区:
             国家 /地区(学生[i] ['位置'])
             holiday_calendar.append('Holiday_' +学生[i] ['location']。下())
    
     print(holiday_calendar)#输出:['Holiday_US','Holiday_sg'...]
    
     对于我的范围(0,Len(Holiday_Calendar)):
    
         打印(Holiday_Calendar [i])
         当地人[holiday_calendar [i]] = json.loads(s3.Object('my_s3_bucket','holiday_folder' + holday_calendar [i] +'.json'))。
     

我的问题是,我不能将holday_calendar列表值转换为变量。当我运行上述代码时,我会遇到此错误:

"errorMessage": "'builtin_function_or_method' object does not support item assignment",
"errorType": "TypeError"

我很确定该错误来自Globals()行,因为我运行了代码的所有其他部分,并且它们都可以完美地工作。我还尝试了许多主题中建议的exec()和当地人()(),但没有奏效。有什么建议吗?

I am currently writing a lambda function (python 3.9) with the objective of finding students who are celebrating holiday in their respective country. To start off, I have a student.json file and a folder containing all the countries' holiday in the holiday_folder in my S3 bucket:

    (main directory)
    student.json
    holiday_folder
     (within holiday_folder)
     holiday_us.json
     holiday_cn.json
     holiday_my.json
     holiday_th.json
     the list goes on...

The student.json file has the following content:

{'student': [{'name':'Adam', 'location':'US'}, {'name':'Ben', 'location':'SG'}...]}

While all the holiday json files are structured similarly as follow:

{ "holiday_us":[ { "date":"20220101", "desc":"New Year's Day" }, {"date"....}]}

To achieve my objective, I:

  1. json.loads student.json content into variable student

  2. Create a list called countries and run a for loop to append only the "location" values of all students in student variable

  3. Create another list called holiday_calendar to store transformed location values a.k.a. transforming ['US', 'SG', ...] into ['holiday_us', 'holiday_sg' ...] so that I can convert these list values into variable names to load holiday dates

  4. Run a conditional for loop on student. The loop will look into the holiday list that's only relevant to the specific student and return value accordingly. I wont be going deep on this as this is not the point of the question.

     countries = []
     holiday_calendar = []
     for i in range(len(student)):
         if student[i]['location'] not in countries:
             countries.append(student[i]['location'])
             holiday_calendar.append('holiday_' + student[i]['location'].lower())
    
     print(holiday_calendar) # output: ['holiday_us', 'holiday_sg'...]
    
     for i in range(0, len(holiday_calendar)):
    
         print(holiday_calendar[i])
         locals[holiday_calendar[i]] = json.loads(s3.Object('my_s3_bucket', 'holiday_folder' + holiday_calendar[i] + '.json')).get()['Body'].read())
    

My issue is that I cannot convert the holiday_calendar list values into variables. When I ran the above code, I got this error:

"errorMessage": "'builtin_function_or_method' object does not support item assignment",
"errorType": "TypeError"

I am pretty sure the error came from the globals() line as I have ran all the other parts of the code and they all worked flawlessly. I have also tried exec() and locals() as per suggested in a lot of SO topics but none worked. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文