繁体   English   中英

检查列表的所有项目是否都是整数字符串

[英]Check if all items of a list are string of integers

我有一个带有问题和答案的 dataframe:

,Unnamed: 0,Unnamed: 0.1,Unnamed: 0.1.1,URL,QID,Questions,Answers,QType
0,0,0,1,When do you think your next vacation can start?,"['In next 3 months', 'In next 6 months', 'In next 1 year', 'Only once COVID-19 is under control', 'Only once COVID-19 vaccine is developed']",
1,1,1,2,What are your preferences regarding medical treatment policy (with additional cost)?,"[""Doctor's availability in hotel"", 'Ventilator availability in hotel', 'Tie-ups with nearby hospitals', 'Availability of medical rooms with primary first aid care']",
2,2,2,3,Preferences of complementary breakfast?,"['Buffet breakfast with social distancing', 'Buffet breakfast replaced with Ala-carte with limited options', 'Breakfast to be delivered in room with limited options (chargeable)', 'Packaged breakfast only']",
3,3,3,4,What is your preference for packaged food?,"['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']",
4,4,4,5,Consumer Personality,[],

我想在“QType”列中获得答案类型。 也就是:“口味量表”或“多项选择”。 味觉等级是一串整数列表,其他都是多项选择:

,Unnamed: 0,Unnamed: 0.1,Unnamed: 0.1.1,URL,QID,Questions,Answers,QType
0,0,0,1,When do you think your next vacation can start?,"['In next 3 months', 'In next 6 months', 'In next 1 year', 'Only once COVID-19 is under control', 'Only once COVID-19 vaccine is developed']",Multiple Choice
1,1,1,2,What are your preferences regarding medical treatment policy (with additional cost)?,"[""Doctor's availability in hotel"", 'Ventilator availability in hotel', 'Tie-ups with nearby hospitals', 'Availability of medical rooms with primary first aid care']",Multiple Choice
2,2,2,3,Preferences of complementary breakfast?,"['Buffet breakfast with social distancing', 'Buffet breakfast replaced with Ala-carte with limited options', 'Breakfast to be delivered in room with limited options (chargeable)', 'Packaged breakfast only']",Multiple Choice
3,3,3,4,What is your preference for packaged food?,"['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']",scale

目前,我尝试了以下方法:

>>>def classifier(l):
...     try: 
...         l = ast.literal_eval(l)
...         if all(type(int(item))==int for item in l):
...             l = list(map(int, l))
...     except ValueError:
...         return None
...     j = 0
...     if not l:
...         return None
...     try: 
...         if all(isinstance(x, int) for x in l):
...             print("Likert Scale")
...             return "Likert Scale"
...         else:
...             print("Mult")
...             return "Multiple Choice"
...     except:
...         print("Exception")
...         return None
... df.QType = df.apply(lambda x: classifier(df.Questions), axis = 1)

也就是说每次只有数字的时候我说题目是数字的尺度,否则就是选择题。

但它返回None因为每一位都有一个 ValueError 。

df.QType
0       None
1       None
2       None
3       None
4       None
        ... 
2953    None
2954    None
2955    None
2956    None
2957    None

谷歌 Forms API

If you work with Google you can use Google Forms API for this instead of BeautifulSoup: https://developers.google.com/apps-script/reference/forms/

This service allows scripts to create, access, and modify Google Forms.

谷歌表格 API

您还可以在电子表格中导出所有响应,并与 Python API 一起使用。 我推荐你这种方式,因为有更多关于这个主题的资源。

在这里您可以找到如何导出回复: https://support.google.com/docs/answer/139706?hl=en&ref_topic=6063592

在这里你可以找到谷歌表格 Python API: https://developers.google.com/sheets/api/quickstart/python

https://github.com/burnash/gspread

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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