将数组从python传输到javascript

将数组从python传输到javascript,javascript,python,arrays,Javascript,Python,Arrays,我有一个test.txt文件,它有两列数据(x和y),例如: 还有一个python程序,读取这些值并将其存储在x和y中,如下所示: #test.py # Read the file. f = open('test.txt', 'r') # read the whole file into a single variable, which is a list of every row of the file. lines = f.readlines() f.close() # initial

我有一个test.txt文件,它有两列数据(x和y),例如:

还有一个python程序,读取这些值并将其存储在x和y中,如下所示:

#test.py
# Read the file.
f = open('test.txt', 'r')

# read the whole file into a single variable, which is a list of every  row of the file.
lines = f.readlines()
f.close()

# initialize some variable to be lists:
x = []
y = []

# scan the rows of the file stored in lines, and put the values into some variables:
for line in lines:
    p = line.split()
    x.append(float(p[0]))
    y.append(float(p[1]))

我想获取存储在x和y中的这些值,并将它们传输到javascript程序中的两个类似数组中,以图形的形式显示它们。如何在python和javascript之间进行转换

不清楚您为什么要传输这个,但是您只需将列表格式化为
var-someList=[[x1,y1],[x2,y2]]
即可将其写入js文件

如果您确实需要维护单独的x和y列表,那么使用“从行中的行创建
将更容易

zippedList = list(list(zipped) for zipped in zip(xList,yList))
print('var someList = {};'.format(zippedList))
这给了你

var someList=[[1,23]、[2,234]、[4,52]、[43,5]、[3,35]

根据需要将其写入一个js文件,或者根据需要附加到现有html

with open('index.html','a') as f:
    f.write('<script> var transferredList={}; </script>'.format(zippedList))
以open('index.html','a')作为f的
:
f、 写入('var transferredList={};'.format(zippedList))

我没有将数组存储在变量x和y中,但如果您至少了解一点JavaScript,就可以这样做

var Request=new XMLHttpRequest();//request object
Request.open("GET","test.txt",true);
/*
 * Open request;
 * Take 2 arguments, but third is optional (async).
 * If you want async, then set onreadystatechange instead of onload event
*/
Request.onreadystatechange=function(){
    if(this.status==400){//status is okay
        var Lines=this.responseText.split("\n");
        alert(Lines[0])//will alert the first line
    }
    /*
     * this.status>=500 are request issues or user network delay
     * this.status=404 means file not found
    */
}

你的问题有点含糊。您的问题有两种可能的解释方式,以下是相应的答案:

  • python代码需要将它们转换为在browser/node.js应用程序中运行的javascript。为了做到这一点,应用程序的Python部分需要要么将数据存储在数据库中,要么通过某种API公开数据,javascript部分可能会使用这种API。更有趣的是,您可以在两个web服务器(例如socket.io)之间建立一个连接,让Python在创建数组时通过该连接发送数组
  • 这就是我相信您正试图基于所发布的代码所做的:在Python中预处理一些数据,然后将其传递给拼图中没有实时方面的其他javascript部分

    在本例中,您只需将数组写入JSON,并用Javascript解析即可。要写入文件,可以执行以下操作:

  • 因为您将“显示”与
    JavaScript
    一起提到,所以我有点假设您的目标是让
    JavaScript
    在web浏览器中显示
    python
    脚本从后端文件读取的数据。从后端
    python
    传输到前端
    JavaScript
    是小菜一碟,因为两种语言都“讲”JSON

    在后端,您将执行以下操作

    import json
    response = json.dumps(response_object)
    
    在前端,您可以立即使用

    var responseObject = JSON.parse(responseString);
    
    前端如何调用后端也很清楚,即使用
    ajax
    ,通常封装在一个方便的库中,如
    jQuery

    当让
    python
    脚本在web服务器中运行时,该字段将打开。一个很好的起点是在web中使用
    python
    。您还必须咨询您的网络托管服务,因为大多数托管服务商都有明确的指导,无论他们是承认
    CGI
    还是例如
    FastCGI
    。一旦清楚了这一点,您可能想看看
    Flask
    微框架或类似的东西,它们提供了许多您需要的现成服务


    我已经提到了这里的所有流行语,以便您自己进行调查;)

    将值存储在数据库中并使用服务器端语言,或者从python调用网页并将其作为参数传递(使用
    GET
    或使用json结构化)
    import json
    response = json.dumps(response_object)
    
    var responseObject = JSON.parse(responseString);