webrequest 对象到龙卷风中的 csv
webrequest object to csv in tornado
我通过上传 csv 生成了以下 webrequest 对象正文:
------WebKitFormBoundaryZfGb9EY1D9p7BTV9
Content-Disposition: form-data; name="file"; filename="vb_bpm.csv"
Content-Type: text/csv
10/05/2015,00:08:17,8,3960,2,52503,19824
10/05/2015,00:19:35,8,3960,2,52542,19824
10/05/2015,02:21:21,8,3960,2,55366,19824
10/05/2015,02:32:11,8,3960,2,55942,19824
10/05/2015,02:43:13,8,3960,2,56139,19824
10/05/2015,02:54:09,8,3960,2,56750,19824
10/05/2015,03:05:08,8,3960,2,57007,19824
10/05/2015,03:16:10,8,3960,2,57040,19824
10/05/2015,03:27:00,8,3960,2,57633,19824
10/05/2015,03:37:54,8,3960,2,58306,19824
10/05/2015,03:48:47,8,3960,2,58984,19824
------WebKitFormBoundaryZfGb9EY1D9p7BTV9--
我想将其保存到一个新的 csv 文件中。
以下给出以上输出:
def csv_handle(self, request, response) :
print request.body()
如有任何帮助,我们将不胜感激。
我建议对所有上传的数据进行严格验证,以防止代码注入和数据质量下降。
我的解决方案是 运行 通过正则表达式上传的行:
import re
lines = str(request.body).split()
p = "\d{1,2}\/\d{1,2}\/\d{2,4},\d{1,2}\:\d{1,2}\:\d{1,2},\d+,\d+,\d+,\d+,\d+"
filtered_body = "\n".join(filter(lambda l: re.match(p, l), lines))
print "Original:\n%s\n" % request.body
print "Filtered:\n%s\n" % filtered_body
您的数据的结果输出:
Original:
------WebKitFormBoundaryZfGb9EY1D9p7BTV9
Content-Disposition: form-data; name="file"; filename="vb_bpm.csv"
Content-Type: text/csv
10/05/2015,00:08:17,8,3960,2,52503,19824
10/05/2015,00:19:35,8,3960,2,52542,19824
10/05/2015,02:21:21,8,3960,2,55366,19824
10/05/2015,02:32:11,8,3960,2,55942,19824
10/05/2015,02:43:13,8,3960,2,56139,19824
10/05/2015,02:54:09,8,3960,2,56750,19824
10/05/2015,03:05:08,8,3960,2,57007,19824
10/05/2015,03:16:10,8,3960,2,57040,19824
10/05/2015,03:27:00,8,3960,2,57633,19824
10/05/2015,03:37:54,8,3960,2,58306,19824
10/05/2015,03:48:47,8,3960,2,58984,19824
------WebKitFormBoundaryZfGb9EY1D9p7BTV9--
Filtered:
10/05/2015,00:08:17,8,3960,2,52503,19824
10/05/2015,00:19:35,8,3960,2,52542,19824
10/05/2015,02:21:21,8,3960,2,55366,19824
10/05/2015,02:32:11,8,3960,2,55942,19824
10/05/2015,02:43:13,8,3960,2,56139,19824
10/05/2015,02:54:09,8,3960,2,56750,19824
10/05/2015,03:05:08,8,3960,2,57007,19824
10/05/2015,03:16:10,8,3960,2,57040,19824
10/05/2015,03:27:00,8,3960,2,57633,19824
10/05/2015,03:37:54,8,3960,2,58306,19824
10/05/2015,03:48:47,8,3960,2,58984,19824
将行保存到文件很简单。
什么是"webrequest object"? Tornado 的请求对象有一个 body 属性,但没有 body 方法。假设您的意思是 Tornado 请求并且在 request.body
上使用括号是一个拼写错误,您可以使用请求的 files
属性。 body
始终是原始请求主体,在本例中包含一个 multipart/form-data
包装器。 Tornado 识别 multipart/form-data
内容类型(对于非流式请求)并将其解析为 files
对象。在这种情况下,您可以在 request.files["file"][0].body
.
下找到您的 csv 文件
我通过上传 csv 生成了以下 webrequest 对象正文:
------WebKitFormBoundaryZfGb9EY1D9p7BTV9
Content-Disposition: form-data; name="file"; filename="vb_bpm.csv"
Content-Type: text/csv
10/05/2015,00:08:17,8,3960,2,52503,19824
10/05/2015,00:19:35,8,3960,2,52542,19824
10/05/2015,02:21:21,8,3960,2,55366,19824
10/05/2015,02:32:11,8,3960,2,55942,19824
10/05/2015,02:43:13,8,3960,2,56139,19824
10/05/2015,02:54:09,8,3960,2,56750,19824
10/05/2015,03:05:08,8,3960,2,57007,19824
10/05/2015,03:16:10,8,3960,2,57040,19824
10/05/2015,03:27:00,8,3960,2,57633,19824
10/05/2015,03:37:54,8,3960,2,58306,19824
10/05/2015,03:48:47,8,3960,2,58984,19824
------WebKitFormBoundaryZfGb9EY1D9p7BTV9--
我想将其保存到一个新的 csv 文件中。 以下给出以上输出:
def csv_handle(self, request, response) :
print request.body()
如有任何帮助,我们将不胜感激。
我建议对所有上传的数据进行严格验证,以防止代码注入和数据质量下降。
我的解决方案是 运行 通过正则表达式上传的行:
import re
lines = str(request.body).split()
p = "\d{1,2}\/\d{1,2}\/\d{2,4},\d{1,2}\:\d{1,2}\:\d{1,2},\d+,\d+,\d+,\d+,\d+"
filtered_body = "\n".join(filter(lambda l: re.match(p, l), lines))
print "Original:\n%s\n" % request.body
print "Filtered:\n%s\n" % filtered_body
您的数据的结果输出:
Original:
------WebKitFormBoundaryZfGb9EY1D9p7BTV9
Content-Disposition: form-data; name="file"; filename="vb_bpm.csv"
Content-Type: text/csv
10/05/2015,00:08:17,8,3960,2,52503,19824
10/05/2015,00:19:35,8,3960,2,52542,19824
10/05/2015,02:21:21,8,3960,2,55366,19824
10/05/2015,02:32:11,8,3960,2,55942,19824
10/05/2015,02:43:13,8,3960,2,56139,19824
10/05/2015,02:54:09,8,3960,2,56750,19824
10/05/2015,03:05:08,8,3960,2,57007,19824
10/05/2015,03:16:10,8,3960,2,57040,19824
10/05/2015,03:27:00,8,3960,2,57633,19824
10/05/2015,03:37:54,8,3960,2,58306,19824
10/05/2015,03:48:47,8,3960,2,58984,19824
------WebKitFormBoundaryZfGb9EY1D9p7BTV9--
Filtered:
10/05/2015,00:08:17,8,3960,2,52503,19824
10/05/2015,00:19:35,8,3960,2,52542,19824
10/05/2015,02:21:21,8,3960,2,55366,19824
10/05/2015,02:32:11,8,3960,2,55942,19824
10/05/2015,02:43:13,8,3960,2,56139,19824
10/05/2015,02:54:09,8,3960,2,56750,19824
10/05/2015,03:05:08,8,3960,2,57007,19824
10/05/2015,03:16:10,8,3960,2,57040,19824
10/05/2015,03:27:00,8,3960,2,57633,19824
10/05/2015,03:37:54,8,3960,2,58306,19824
10/05/2015,03:48:47,8,3960,2,58984,19824
将行保存到文件很简单。
什么是"webrequest object"? Tornado 的请求对象有一个 body 属性,但没有 body 方法。假设您的意思是 Tornado 请求并且在 request.body
上使用括号是一个拼写错误,您可以使用请求的 files
属性。 body
始终是原始请求主体,在本例中包含一个 multipart/form-data
包装器。 Tornado 识别 multipart/form-data
内容类型(对于非流式请求)并将其解析为 files
对象。在这种情况下,您可以在 request.files["file"][0].body
.