I am formatting table data in HTML and sending it to Python using this script:
我正在格式化HTML中的表数据并使用此脚本将其发送到Python:
var html_table_data = "";
var bRowStarted = true;
var count1 = 1
$('#woTable tbody>tr').each(function () {
if (count1 != 1) {
html_table_data += "['";
}
else
html_table_data += "[";
$('td', this).each(function () {
if (html_table_data.length == 0 || bRowStarted == true) {
html_table_data += $(this).text();
bRowStarted = false;
}
else
html_table_data += "', '" + $(this).text();
});
if (count1 != 1) {
html_table_data += "'], ";
}
count1 ++
bRowStarted = true;
});
html_table_data = html_table_data.slice(0, -2); // remove comma and space I added above for last element
html_table_data += "]"; // finish off the list
alert(html_table_data);
$.ajax({
url: '/plan_update/',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(html_table_data),
type: 'POST'
Python receives that data which looks like a well formatted list however using the code below it tries to process as string (character by character) rather than a multi-dimensional list with each line having 9 data elements. In the above I have replaced the comma between each line of data with a \n but still get the same result. In python I have tried without the literal_eval with similar results. The python code is:
Python接收的数据看起来像一个格式良好的列表,但是使用下面的代码它尝试以字符串(逐个字符)处理,而不是每行包含9个数据元素的多维列表。在上面我已经用\ n替换了每行数据之间的逗号,但仍然得到相同的结果。在python中我尝试了没有literal_eval类似的结果。 python代码是:
plantable = json.loads(request.data)
for lines in plantable:
plantable2 = literal_eval(plantable)
Time = (plantable2[4])
Quantity = (plantable2[5])
Reps = (plantable2[6])
Rest = (plantable2[7])
Seq = (plantable2[8])
Can you please provide some guidance on formatting the HTML data differently or processing the existing data in Python so I can process each line and data element within each line. Obviously I'm a newby so any guidance is appreciated.
您能否提供一些有关格式化HTML数据或以Python处理现有数据的指导,以便我可以处理每行中的每一行和数据元素。显然我是新手,所以任何指导都表示赞赏。
2 个解决方案
#1
0
Your issue is that you're building a string to look like JSON, rather than building an actual nested array object. Then you're sending it to the server and it's processing it as a string.
您的问题是您正在构建一个看起来像JSON的字符串,而不是构建一个实际的嵌套数组对象。然后你将它发送到服务器并将其作为字符串处理。
Instead, build the data structure in Javascript as the multi-dimensional array, like so:
相反,在Javascript中构建数据结构作为多维数组,如下所示:
var html_table_data = []
$('#woTable tbody>tr').each(function () {
table_row_data = [];
$('td', this).each(function () {
table_row_data.push($(this).text());
});
if (table_row_data.length > 0) { // Only add a row if it's not empty
html_table_data.push(table_row_data);
}
});
Then you can send html_table_data
using your AJAX request as-is (keep the JSON.stringify
).
然后你可以按原样使用你的AJAX请求发送html_table_data(保留JSON.stringify)。
The Python code also needs to be changed. literal_eval
should be removed. json.loads()
already converts the data from the request into a usable Python structure, so literal_eval
will fail.
Python代码也需要更改。应删除literal_eval。 json.loads()已经将请求中的数据转换为可用的Python结构,因此literal_eval将失败。
JSON is meant to transfer objects as text strings. Use the tools to do that, rather than trying to build the strings from scratch. As you can see, it's a lot simpler and less painful.
JSON旨在将对象作为文本字符串传输。使用工具来做到这一点,而不是尝试从头开始构建字符串。正如你所看到的,它更简单,更少痛苦。
#2
0
There are few errors here.
这里几乎没有错误。
- You are already creating json string in your JS. When calling
JSON.stringify
you're "double jsoning" thing, so removeJSON.stringify
- 您已经在JS中创建了json字符串。在调用JSON.stringify时,你是“双重jsoning”的东西,所以删除JSON.stringify
- As you're sending content type to be
application/json
, flasksrequest
havejson
property - 当您将内容类型发送为application / json时,flask请求具有json属性
So, your ajax should be:
所以,你的ajax应该是:
$.ajax({
url: '/plan_update/',
contentType: "application/json; charset=utf-8",
data: html_table_data,
dataType: "json",
type: 'POST'
});
And python code should be like:
和python代码应该像:
import json
plantable = json.loads(request.json)
for line in plantable:
...
#1
0
Your issue is that you're building a string to look like JSON, rather than building an actual nested array object. Then you're sending it to the server and it's processing it as a string.
您的问题是您正在构建一个看起来像JSON的字符串,而不是构建一个实际的嵌套数组对象。然后你将它发送到服务器并将其作为字符串处理。
Instead, build the data structure in Javascript as the multi-dimensional array, like so:
相反,在Javascript中构建数据结构作为多维数组,如下所示:
var html_table_data = []
$('#woTable tbody>tr').each(function () {
table_row_data = [];
$('td', this).each(function () {
table_row_data.push($(this).text());
});
if (table_row_data.length > 0) { // Only add a row if it's not empty
html_table_data.push(table_row_data);
}
});
Then you can send html_table_data
using your AJAX request as-is (keep the JSON.stringify
).
然后你可以按原样使用你的AJAX请求发送html_table_data(保留JSON.stringify)。
The Python code also needs to be changed. literal_eval
should be removed. json.loads()
already converts the data from the request into a usable Python structure, so literal_eval
will fail.
Python代码也需要更改。应删除literal_eval。 json.loads()已经将请求中的数据转换为可用的Python结构,因此literal_eval将失败。
JSON is meant to transfer objects as text strings. Use the tools to do that, rather than trying to build the strings from scratch. As you can see, it's a lot simpler and less painful.
JSON旨在将对象作为文本字符串传输。使用工具来做到这一点,而不是尝试从头开始构建字符串。正如你所看到的,它更简单,更少痛苦。
#2
0
There are few errors here.
这里几乎没有错误。
- You are already creating json string in your JS. When calling
JSON.stringify
you're "double jsoning" thing, so removeJSON.stringify
- 您已经在JS中创建了json字符串。在调用JSON.stringify时,你是“双重jsoning”的东西,所以删除JSON.stringify
- As you're sending content type to be
application/json
, flasksrequest
havejson
property - 当您将内容类型发送为application / json时,flask请求具有json属性
So, your ajax should be:
所以,你的ajax应该是:
$.ajax({
url: '/plan_update/',
contentType: "application/json; charset=utf-8",
data: html_table_data,
dataType: "json",
type: 'POST'
});
And python code should be like:
和python代码应该像:
import json
plantable = json.loads(request.json)
for line in plantable:
...