- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
Python中有没有类似trim()的函数?
我使用 Flask 迷你框架,但它不接受:
selected_student = (request.args.get('student_form')).strip()
其错误:AttributeError:'NoneType'对象没有属性'strip'
selected_student.replace("", "")
其错误:AttributeError:'NoneType'对象没有属性'replace'
我需要一个像trim()这样的函数,而不需要编写类/子类或javascript
最佳答案
您所看到的错误是因为没有数据从您的表单传递到 Flask 服务器。您对 request
的使用将返回 None
值类型,而不是 str
。
您为表单发布了以下 HTML 标记:
<form action="/student" method='POST'>
<select name="student_form ">
{% for student in students_list %}
<option value="{{student}}">{{student}}</option>
{% endfor %}
</select>
<input type='submit' value='submit' />
</form>
因此,您需要让 Flask 在服务器端获取这些数据,例如:
@app.route('/student', methods=['POST'])
def receive_student_form_data:
my_selection = str(request.form.get('student_form')).strip()
print(my_selection)
只是为了澄清为什么我以这种方式创建我的方法:我注意到您正在使用 request.args.get()
来检索表单发送的值。这是不正确的。
request.args
用于从 URL 检索键/值对。request.form
用于从 HTML 表单中检索键/值对。
所以我建议您应该使用request.form.get('student_form')
。如果您确实想确定 Flask 服务器检索它时是否将其转换为 str
,那么您可以将其转换为 str
,如下所示:
str(request.form.get('student_form'))
然后,正如一些人已经建议的那样,您可以使用 .strip()
方法删除任何尾随空格。
关于python - 使用 Flask 在 Python 中修剪() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47568565/
在字符串 (\test.something) 之后删除所有内容的命令是什么。 我在文本文件中有信息,但是在字符串之后有 1000 行我不想要的文本。如何删除包括字符串在内的所有内容。 这就是我所拥有的
我想删除每个项目的空白.amount 我在 .amount 类上使用 trim 和 each,但它似乎不起作用: jQuery('.amount').each(function(){ jQue
我列出了以下正在稳步增加的点,例如: [[0, 0], [9, 4], [18, 19], [25, 34], [48, 48], [54, 53], [61, 65], [69, 82], [73,
清理自动生成的 html 带来更多乐趣。标签中注入(inject)了大量无关的空格: Lorem Ipsum dolor sit... ( 代表实际空间,而不是实
计算 trimmed 的有效方法是什么?或winsorized列表的标准差? 我不介意使用numpy,但如果我必须制作列表的单独副本,它会非常慢。 最佳答案 这将制作两个副本,但您应该尝试一下,因为它
这个问题在这里已经有了答案: 关闭10 年前。 Possible Duplicate: Leave only two decimal places after the dot Formatting
我正在使用绑定(bind)来填充 Listbox,其中包含 TextBlock 等。 问题是: 如何确保绑定(bind)到 TextBlock 的 Text 属性的文本具有特定长度,或者它是显示为某些
我正在按以下方式修剪 NSString: NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:
我有一个文本列,其内容在字符串的前后混合了换行符和空白字符。我正在尝试编写一个 SELECT 语句,它向我显示没有前导和尾随垃圾的内容。 以下查询修剪空格: SELECT TRIM(column) F
这个问题在这里已经有了答案: How to slice a pandas DataFrame by position? (5 个答案) 关闭 5 年前。 我似乎看不出这里有什么问题。我有一个长度为
我没有找到类似的问题可能是因为我没有找到正确的词(英语不是我的母语) 问题 我有一个 varchar 值,末尾有一个空格:"opt-193-381-markets " 当我执行 SELECT 的值没有
假设我有 $url="../folder/file",我想找到并删除 ../ 部分。 我正在使用 trim() ...... $url = trim($url,"../"); ……但它给了我一个警告:
这个问题在这里已经有了答案: Java String trim has no effect (7 个答案) string trim function is not working [closed]
我有以下 XML: tag:search.twitter.com,2005:22204349686 如何将第二个冒号后的所有内容写入变量? 例如22204349686 最佳答案 if(preg_mat
修剪在深度优先搜索中什么时候停止有效?我一直在研究一种有效的方法来解决 N-Queens 问题,并且我第一次关注修剪。我已经为前两行实现了它,但它什么时候停止有效?我应该修剪多远? 最佳答案 N 皇后
我有一个图表,按天将两种类型的数据制成表格,我希望只修剪图表中的第一个和最后一个标签。这是一个可重现的数据示例: library(dplyr) library(ggplot2) library(sca
如何去掉 excel 中的前导空格? 我有很多行有这个问题。 最佳答案 在您的空格删除请求中,请注意: TRIM仅删除字符 32,即标准空格。 CLEAN将删除非打印空格,例如回车符(字符 13)和换
当前正在编写指令,并且需要将空格作为字符传递给它。 喜欢: 结果证明 angular 消除了前导空间;但我想保留它。 有什么办法吗? 编辑:我将指令参数作为字符串传递(使用@,而不是作为变量,使用=
我正在为一个使用 Bold for Delphi 对象持久性框架的应用程序的数据库做一些维护。该数据库已经投入生产多年,其中一些表已经变得非常大。其中之一是 BOLD_CLOCKLOG这与 Bold
如何“修剪” NSString 以便仅用旧字符串的特定部分创建新字符串? 例如,我有字符串“Monday the 12th of September”,我如何仅选出“Monday”部分? 最佳答案 使
我是一名优秀的程序员,十分优秀!