无法将字符串转换为变量以进行dict值分配?
我目前正在撰写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:
json.loads
student.json
content to variableStudent> Student
P>创建一个名为
code> nistry
的列表,并运行A用于循环以仅附加“位置” “学生中所有学生的价值
创建另一个名为
holday_calendar
student
变量的“位置”值/code>要存储转换的位置值aka转换['us',sg',...]到['holday_us','holday_sg'...],以便我可以将这些列表值转换为可变名称以加载假期日期在
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:
json.loads
student.json
content into variablestudent
Create a list called
countries
and run a for loop to append only the "location" values of all students instudent
variableCreate 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 datesRun 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论