Python-写入javascript api

Python-写入javascript api,python,Python,我有一个包含以下内容的.js文件: function get_test1_data() { return [ ['2016-02-12','32765','87917',-55152,'2016-04-27','10:52'], ['2016-02-19','32765','87917',-55152,'2016-04-27','10:52'], ['2016-02-26','32765','87917',-55152,'2016-04-27','10:52'], ['2016-03-04'

我有一个包含以下内容的.js文件:

function get_test1_data() {
  return [
['2016-02-12','32765','87917',-55152,'2016-04-27','10:52'],
['2016-02-19','32765','87917',-55152,'2016-04-27','10:52'],
['2016-02-26','32765','87917',-55152,'2016-04-27','10:52'],
['2016-03-04','32765','87917',-55152,'2016-04-27','10:52'],
['2016-03-11','40431','89100',-48669,'2016-05-02','14:54'],
['2016-03-18','40431','89100',-48669,'2016-05-02','14:56'],
['2016-03-25','40431','89100',-48669,'2016-05-02','15:59'],
['2016-04-01','40431','89100',-48669,'2016-05-02','16:29'],
['2016-04-08','38557','71392',-32835,'2016-06-02','19:08'],
['2016-04-15','38557','71392',-32835,'2016-06-02','19:11'],
['2016-04-22','38557','71392',-32835,'2016-06-02','19:13'],
['2016-04-29','38557','71392',-32835,'2016-06-02','19:15'],
['2016-05-06','38557','71392',-32835,'2016-06-02','19:16'],
['2016-05-13','38557','71392',-32835,'2016-06-02','19:17'],
['2016-05-20','38557','71392',-32835,'2016-06-02','19:18'],
['2016-05-27','38557','71392',-32835,'2016-06-02','19:20'],
['2016-06-03','38557','71392',-32835,'2016-06-02','19:27'],
['2016-06-10','38557','71392',-32835,'2016-06-02','20:45'],
['2016-06-17','38557','71392',-32835,'2016-06-02','20:46'],
  ];
}
我有一个Python3脚本,它偶尔会生成上面的一行代码

'2016-02-12','32765','87917',-55152,'2016-04-27','10:52'
通常我会在.csv文件中添加行。为此,我使用以下代码:

fd = open('test.csv','a')
fd.write(z + "\n")
fd.close()
由于.csv不包含函数,因此代码按预期工作。 我需要开始在.js文件中添加相同的行。出于这个原因,我修改了我的脚本。新行如下所示:

['2016-02-12','32765','87917',-55152,'2016-04-27','10:52'],
tup = (d2(test),a,b,c,st,st1)
zz = str(tup)
zz = ''.join(zz)
zz = zz.replace("(","[")
zz = zz.replace(")","]")
zz = zz.replace(" ","")
zz = str(zz)
print (zz)

data = zz

# Make a string representing the `row` list with no space between items,
# terminate the string with ",\n"     
def format_row(row):
    s = ','.join(['{!r}'.format(u) for u in row])
    return '[{}],\n'.format(s)

def update_js(fname, data):
    # Read old function source
    with open(fname, 'r') as f:
        lines = f.readlines()
    body, tail = lines[:-2], lines[-2:]

    # Convert data rows to strings and add to body list
    for row in data:
        body.append(format_row(row))

    # Save new function source
    with open(fname, 'w') as f:
        f.writelines(body + tail)

update_js('test1.js', data)
function get_test1_data() {
  return [
['2016-02-12','32765','87917',-55152,'2016-04-27','10:52'],
['2016-02-19','32765','87917',-55152,'2016-04-27','10:52'],
['2016-02-26','32765','87917',-55152,'2016-04-27','10:52'],
['2016-03-04','32765','87917',-55152,'2016-04-27','10:52'],
['2016-03-11','40431','89100',-48669,'2016-05-02','14:54'],
['2016-03-18','40431','89100',-48669,'2016-05-02','14:56'],
['2016-03-25','40431','89100',-48669,'2016-05-02','15:59'],
['2016-04-01','40431','89100',-48669,'2016-05-02','16:29'],
['2016-04-08','38557','71392',-32835,'2016-06-02','19:08'],
['2016-04-15','38557','71392',-32835,'2016-06-02','19:11'],
['2016-04-22','38557','71392',-32835,'2016-06-02','19:13'],
['2016-04-29','38557','71392',-32835,'2016-06-02','19:15'],
['2016-05-06','38557','71392',-32835,'2016-06-02','19:16'],
['2016-05-13','38557','71392',-32835,'2016-06-02','19:17'],
['2016-05-20','38557','71392',-32835,'2016-06-02','19:18'],
['2016-05-27','38557','71392',-32835,'2016-06-02','19:20'],
['2016-06-03','38557','71392',-32835,'2016-06-02','19:27'],
['2016-06-10','38557','71392',-32835,'2016-06-02','20:45'],
['2016-06-17','38557','71392',-32835,'2016-06-02','20:46'],
['2016-07-08','38557','71392',-32835,'2016-06-02','22:07'],
['2016-07-15','38557','71392',-32835,'2016-06-02','22:18'],
['2016-07-08','38557','71392',-32835,'2016-06-02','22:07'],
['2016-07-08','38557','71392',-32835,'2016-06-02','22:07'],
['2016-07-08','38557','71392',-32835,'2016-06-02','22:07'],
['['],
["'"],
['2'],
['0'],
['1'],
['7'],
['-'],
['0'],
['1'],
['-'],
['2'],
['0'],
["'"],
[','],
["'"],
['3'],
['8'],
['5'],
['5'],
['7'],
["'"],
[','],
["'"],
['7'],
['1'],
['3'],
['9'],
['2'],
["'"],
[','],
['-'],
['3'],
['2'],
['8'],
['3'],
['5'],
[','],
["'"],
['2'],
['0'],
['1'],
['6'],
['-'],
['0'],
['6'],
['-'],
['0'],
['3'],
["'"],
[','],
["'"],
['1'],
['5'],
[':'],
['4'],
['6'],
["'"],
[']'],
['['],
["'"],
['2'],
['0'],
['1'],
['7'],
['-'],
['0'],
['1'],
['-'],
['2'],
['7'],
["'"],
[','],
["'"],
['3'],
['8'],
['5'],
['5'],
['7'],
["'"],
[','],
["'"],
['7'],
['1'],
['3'],
['9'],
['2'],
["'"],
[','],
['-'],
['3'],
['2'],
['8'],
['3'],
['5'],
[','],
["'"],
['2'],
['0'],
['1'],
['6'],
['-'],
['0'],
['6'],
['-'],
['0'],
['3'],
["'"],
[','],
["'"],
['1'],
['6'],
[':'],
['0'],
['3'],
["'"],
[']'],
];
}
我尝试使用相同的代码

fd = open('test1.js','a')
fd.write(zz + "," + "\n")
fd.close()
但正如所料,在文件末尾添加了新行:

function get_test1_data() {
  return [
['2016-02-12','32765','87917',-55152,'2016-04-27','10:52'],
['2016-02-19','32765','87917',-55152,'2016-04-27','10:52'],
['2016-02-26','32765','87917',-55152,'2016-04-27','10:52'],
['2016-03-04','32765','87917',-55152,'2016-04-27','10:52'],
['2016-03-11','40431','89100',-48669,'2016-05-02','14:54'],
['2016-03-18','40431','89100',-48669,'2016-05-02','14:56'],
['2016-03-25','40431','89100',-48669,'2016-05-02','15:59'],
['2016-04-01','40431','89100',-48669,'2016-05-02','16:29'],
['2016-04-08','38557','71392',-32835,'2016-06-02','19:08'],
['2016-04-15','38557','71392',-32835,'2016-06-02','19:11'],
['2016-04-22','38557','71392',-32835,'2016-06-02','19:13'],
['2016-04-29','38557','71392',-32835,'2016-06-02','19:15'],
['2016-05-06','38557','71392',-32835,'2016-06-02','19:16'],
['2016-05-13','38557','71392',-32835,'2016-06-02','19:17'],
['2016-05-20','38557','71392',-32835,'2016-06-02','19:18'],
['2016-05-27','38557','71392',-32835,'2016-06-02','19:20'],
['2016-06-03','38557','71392',-32835,'2016-06-02','19:27'],
['2016-06-10','38557','71392',-32835,'2016-06-02','20:45'],
['2016-06-17','38557','71392',-32835,'2016-06-02','20:46'],
  ];
}

['2016-07-08','38557','71392',-32835,'2016-06-02','22:07'],
['2016-07-15','38557','71392',-32835,'2016-06-02','22:18'],
任何关于如何修改我的代码、如何开始在需要添加行的地方添加行的建议,我们都将不胜感激

更新

为了将脚本输出定义为数据,我修改了如下代码:

['2016-02-12','32765','87917',-55152,'2016-04-27','10:52'],
tup = (d2(test),a,b,c,st,st1)
zz = str(tup)
zz = ''.join(zz)
zz = zz.replace("(","[")
zz = zz.replace(")","]")
zz = zz.replace(" ","")
zz = str(zz)
print (zz)

data = zz

# Make a string representing the `row` list with no space between items,
# terminate the string with ",\n"     
def format_row(row):
    s = ','.join(['{!r}'.format(u) for u in row])
    return '[{}],\n'.format(s)

def update_js(fname, data):
    # Read old function source
    with open(fname, 'r') as f:
        lines = f.readlines()
    body, tail = lines[:-2], lines[-2:]

    # Convert data rows to strings and add to body list
    for row in data:
        body.append(format_row(row))

    # Save new function source
    with open(fname, 'w') as f:
        f.writelines(body + tail)

update_js('test1.js', data)
function get_test1_data() {
  return [
['2016-02-12','32765','87917',-55152,'2016-04-27','10:52'],
['2016-02-19','32765','87917',-55152,'2016-04-27','10:52'],
['2016-02-26','32765','87917',-55152,'2016-04-27','10:52'],
['2016-03-04','32765','87917',-55152,'2016-04-27','10:52'],
['2016-03-11','40431','89100',-48669,'2016-05-02','14:54'],
['2016-03-18','40431','89100',-48669,'2016-05-02','14:56'],
['2016-03-25','40431','89100',-48669,'2016-05-02','15:59'],
['2016-04-01','40431','89100',-48669,'2016-05-02','16:29'],
['2016-04-08','38557','71392',-32835,'2016-06-02','19:08'],
['2016-04-15','38557','71392',-32835,'2016-06-02','19:11'],
['2016-04-22','38557','71392',-32835,'2016-06-02','19:13'],
['2016-04-29','38557','71392',-32835,'2016-06-02','19:15'],
['2016-05-06','38557','71392',-32835,'2016-06-02','19:16'],
['2016-05-13','38557','71392',-32835,'2016-06-02','19:17'],
['2016-05-20','38557','71392',-32835,'2016-06-02','19:18'],
['2016-05-27','38557','71392',-32835,'2016-06-02','19:20'],
['2016-06-03','38557','71392',-32835,'2016-06-02','19:27'],
['2016-06-10','38557','71392',-32835,'2016-06-02','20:45'],
['2016-06-17','38557','71392',-32835,'2016-06-02','20:46'],
['2016-07-08','38557','71392',-32835,'2016-06-02','22:07'],
['2016-07-15','38557','71392',-32835,'2016-06-02','22:18'],
['2016-07-08','38557','71392',-32835,'2016-06-02','22:07'],
['2016-07-08','38557','71392',-32835,'2016-06-02','22:07'],
['2016-07-08','38557','71392',-32835,'2016-06-02','22:07'],
['['],
["'"],
['2'],
['0'],
['1'],
['7'],
['-'],
['0'],
['1'],
['-'],
['2'],
['0'],
["'"],
[','],
["'"],
['3'],
['8'],
['5'],
['5'],
['7'],
["'"],
[','],
["'"],
['7'],
['1'],
['3'],
['9'],
['2'],
["'"],
[','],
['-'],
['3'],
['2'],
['8'],
['3'],
['5'],
[','],
["'"],
['2'],
['0'],
['1'],
['6'],
['-'],
['0'],
['6'],
['-'],
['0'],
['3'],
["'"],
[','],
["'"],
['1'],
['5'],
[':'],
['4'],
['6'],
["'"],
[']'],
['['],
["'"],
['2'],
['0'],
['1'],
['7'],
['-'],
['0'],
['1'],
['-'],
['2'],
['7'],
["'"],
[','],
["'"],
['3'],
['8'],
['5'],
['5'],
['7'],
["'"],
[','],
["'"],
['7'],
['1'],
['3'],
['9'],
['2'],
["'"],
[','],
['-'],
['3'],
['2'],
['8'],
['3'],
['5'],
[','],
["'"],
['2'],
['0'],
['1'],
['6'],
['-'],
['0'],
['6'],
['-'],
['0'],
['3'],
["'"],
[','],
["'"],
['1'],
['6'],
[':'],
['0'],
['3'],
["'"],
[']'],
];
}
这是
print(zz)

现在的问题是,代码不能将其视为一行,而是将其视为单独的符号。因此,它像这样附加它:

['2016-02-12','32765','87917',-55152,'2016-04-27','10:52'],
tup = (d2(test),a,b,c,st,st1)
zz = str(tup)
zz = ''.join(zz)
zz = zz.replace("(","[")
zz = zz.replace(")","]")
zz = zz.replace(" ","")
zz = str(zz)
print (zz)

data = zz

# Make a string representing the `row` list with no space between items,
# terminate the string with ",\n"     
def format_row(row):
    s = ','.join(['{!r}'.format(u) for u in row])
    return '[{}],\n'.format(s)

def update_js(fname, data):
    # Read old function source
    with open(fname, 'r') as f:
        lines = f.readlines()
    body, tail = lines[:-2], lines[-2:]

    # Convert data rows to strings and add to body list
    for row in data:
        body.append(format_row(row))

    # Save new function source
    with open(fname, 'w') as f:
        f.writelines(body + tail)

update_js('test1.js', data)
function get_test1_data() {
  return [
['2016-02-12','32765','87917',-55152,'2016-04-27','10:52'],
['2016-02-19','32765','87917',-55152,'2016-04-27','10:52'],
['2016-02-26','32765','87917',-55152,'2016-04-27','10:52'],
['2016-03-04','32765','87917',-55152,'2016-04-27','10:52'],
['2016-03-11','40431','89100',-48669,'2016-05-02','14:54'],
['2016-03-18','40431','89100',-48669,'2016-05-02','14:56'],
['2016-03-25','40431','89100',-48669,'2016-05-02','15:59'],
['2016-04-01','40431','89100',-48669,'2016-05-02','16:29'],
['2016-04-08','38557','71392',-32835,'2016-06-02','19:08'],
['2016-04-15','38557','71392',-32835,'2016-06-02','19:11'],
['2016-04-22','38557','71392',-32835,'2016-06-02','19:13'],
['2016-04-29','38557','71392',-32835,'2016-06-02','19:15'],
['2016-05-06','38557','71392',-32835,'2016-06-02','19:16'],
['2016-05-13','38557','71392',-32835,'2016-06-02','19:17'],
['2016-05-20','38557','71392',-32835,'2016-06-02','19:18'],
['2016-05-27','38557','71392',-32835,'2016-06-02','19:20'],
['2016-06-03','38557','71392',-32835,'2016-06-02','19:27'],
['2016-06-10','38557','71392',-32835,'2016-06-02','20:45'],
['2016-06-17','38557','71392',-32835,'2016-06-02','20:46'],
['2016-07-08','38557','71392',-32835,'2016-06-02','22:07'],
['2016-07-15','38557','71392',-32835,'2016-06-02','22:18'],
['2016-07-08','38557','71392',-32835,'2016-06-02','22:07'],
['2016-07-08','38557','71392',-32835,'2016-06-02','22:07'],
['2016-07-08','38557','71392',-32835,'2016-06-02','22:07'],
['['],
["'"],
['2'],
['0'],
['1'],
['7'],
['-'],
['0'],
['1'],
['-'],
['2'],
['0'],
["'"],
[','],
["'"],
['3'],
['8'],
['5'],
['5'],
['7'],
["'"],
[','],
["'"],
['7'],
['1'],
['3'],
['9'],
['2'],
["'"],
[','],
['-'],
['3'],
['2'],
['8'],
['3'],
['5'],
[','],
["'"],
['2'],
['0'],
['1'],
['6'],
['-'],
['0'],
['6'],
['-'],
['0'],
['3'],
["'"],
[','],
["'"],
['1'],
['5'],
[':'],
['4'],
['6'],
["'"],
[']'],
['['],
["'"],
['2'],
['0'],
['1'],
['7'],
['-'],
['0'],
['1'],
['-'],
['2'],
['7'],
["'"],
[','],
["'"],
['3'],
['8'],
['5'],
['5'],
['7'],
["'"],
[','],
["'"],
['7'],
['1'],
['3'],
['9'],
['2'],
["'"],
[','],
['-'],
['3'],
['2'],
['8'],
['3'],
['5'],
[','],
["'"],
['2'],
['0'],
['1'],
['6'],
['-'],
['0'],
['6'],
['-'],
['0'],
['3'],
["'"],
[','],
["'"],
['1'],
['6'],
[':'],
['0'],
['3'],
["'"],
[']'],
];
}
我试图通过使用

zz = ''.join(zz)
zz = str(zz)

但是没有成功。

尽管您的实现对于javascript文件来说不是一个很好的主意,但是您可能希望查看
文件。seek
以二进制模式打开文件,以允许从文件末尾以负偏移量进行查找。但是以字节为单位找到正确的偏移量已经够难了


我建议您将文件的全部内容读入python,修改然后重写。

虽然您的实现对于javascript文件来说不是一个很好的主意,但您可能希望查看
文件。在以二进制模式打开文件的情况下,查找
,以允许从文件末尾以负偏移量进行查找。但是以字节为单位找到正确的偏移量已经够难了


我建议您将文件的全部内容读入python,修改然后重写。

正如我在评论中提到的,您可能应该为此使用JSON。在程序源代码中嵌入大量数据确实不是好的做法。但无论如何

正如Moses Koledoye所建议的那样,实现这一点的方法(不使用JSON)是将.js文件的行读入列表,修改该列表,然后将其保存回.js文件。我们需要在.js文件的最后两行之前追加新数据。一个简单的方法就是

  • 将行列表分为两个列表,一个主体列表和一个尾部列表
  • 将新数据字符串附加到正文列表
  • 将新的body加tail写入文件
  • 以下代码适用于Python2.7和Python3

    data = [
        ['2016-07-08', '38557', '71392', -32835, '2016-06-02', '22:07'],
        ['2016-07-15', '38557', '71392', -32835, '2016-06-02', '22:18'],
    ]
    
    # Make a string representing the `row` list with no space between items,
    # terminate the string with ",\n"     
    def format_row(row):
        s = ','.join(['{!r}'.format(u) for u in row])
        return '[{}],\n'.format(s)
    
    def update_js(fname, data):
        # Read old function source
        with open(fname, 'r') as f:
            lines = f.readlines()
        body, tail = lines[:-2], lines[-2:]
    
        # Convert data rows to strings and add to body list
        for row in data:
            body.append(format_row(row))
    
        # Save new function source
        with open(fname, 'w') as f:
            f.writelines(body + tail)
    
    update_js('test1.js', data)
    

    更新 从您的评论和问题的更新来看,您一次只能在
    .js
    文件中添加一行。这是我之前函数的一个简化版本来完成这个任务。请注意我是如何将数据传递给它的

    def update_js(fname, row):
        # Read old function source
        with open(fname, 'r') as f:
            lines = f.readlines()
        body, tail = lines[:-2], lines[-2:]
    
        # Convert data row to string and add to body list
        s = ','.join(['{!r}'.format(u) for u in row])
        body.append('[{}],\n'.format(s))
    
        # Save new function source
        with open(fname, 'w') as f:
            f.writelines(body + tail)
    
    d2_test = '2016-07-08'
    a, b, c = '38557', '71392', -32835
    st, st1 = '2016-06-02', '22:07'
    
    row = [d2_test, a, b, c, st, st1]
    print(row)
    update_js('test1.js', row)
    

    正如我在评论中提到的,您可能应该为此使用JSON。在程序源代码中嵌入大量数据确实不是好的做法。但无论如何

    正如Moses Koledoye所建议的那样,实现这一点的方法(不使用JSON)是将.js文件的行读入列表,修改该列表,然后将其保存回.js文件。我们需要在.js文件的最后两行之前追加新数据。一个简单的方法就是

  • 将行列表分为两个列表,一个主体列表和一个尾部列表
  • 将新数据字符串附加到正文列表
  • 将新的body加tail写入文件
  • 以下代码适用于Python2.7和Python3

    data = [
        ['2016-07-08', '38557', '71392', -32835, '2016-06-02', '22:07'],
        ['2016-07-15', '38557', '71392', -32835, '2016-06-02', '22:18'],
    ]
    
    # Make a string representing the `row` list with no space between items,
    # terminate the string with ",\n"     
    def format_row(row):
        s = ','.join(['{!r}'.format(u) for u in row])
        return '[{}],\n'.format(s)
    
    def update_js(fname, data):
        # Read old function source
        with open(fname, 'r') as f:
            lines = f.readlines()
        body, tail = lines[:-2], lines[-2:]
    
        # Convert data rows to strings and add to body list
        for row in data:
            body.append(format_row(row))
    
        # Save new function source
        with open(fname, 'w') as f:
            f.writelines(body + tail)
    
    update_js('test1.js', data)
    

    更新 从您的评论和问题的更新来看,您一次只能在
    .js
    文件中添加一行。这是我之前函数的一个简化版本来完成这个任务。请注意我是如何将数据传递给它的

    def update_js(fname, row):
        # Read old function source
        with open(fname, 'r') as f:
            lines = f.readlines()
        body, tail = lines[:-2], lines[-2:]
    
        # Convert data row to string and add to body list
        s = ','.join(['{!r}'.format(u) for u in row])
        body.append('[{}],\n'.format(s))
    
        # Save new function source
        with open(fname, 'w') as f:
            f.writelines(body + tail)
    
    d2_test = '2016-07-08'
    a, b, c = '38557', '71392', -32835
    st, st1 = '2016-06-02', '22:07'
    
    row = [d2_test, a, b, c, st, st1]
    print(row)
    update_js('test1.js', row)
    

    因此,为了清楚起见,您需要将列表添加到括号中返回的列表中?您可能应该使用它而不是CSV。我需要在函数中添加行,而不是在文件末尾。问题是“append”会添加到文件的末尾。如果我将其附加到.csv中,就可以了,因为没有函数。仅限线路。在.js文件中有一个函数,它在末尾添加了两个以上的simbol。这使得附加变得有点复杂。您应该使用三个文件one.js second.json和python脚本。让js文件读取JSON文件。让python附加到JSON文件中。因此,为了清楚起见,您需要在括号中返回的列表中添加列表?您可能应该使用它而不是CSV。我需要在函数中添加行,而不是在文件的末尾。问题是“append”会添加到文件的末尾。如果我将其附加到.csv中,就可以了,因为没有函数。仅限线路。在.js文件中有一个函数,它在末尾添加了两个以上的simbol。这使得附加变得有点复杂。您应该使用三个文件one.js second.json和python脚本。让js文件读取JSON文件。将python附加到JSON文件中。感谢您的帮助。当数据是这样定义的时候,它非常有用:
    data=[[2016-07-08',38557',71392',-32835',2016-06-02',22:07'],[2016-07-15',38557',71392',-32835',2016-06-02',22:18',][/code>为了将我的输出定义为数据,我修改了如下代码:
    tup=(d2(test a,b,c,st,st1)zz str(tup)zzzz=zz.replace(“(”,“[”)zz=zz.replace(“),“])zz=zz.replace(“,”)zz=str(zz)print(zz)data=zz
    这是
    print(zz)
    -
    ['2017-01-27'、'38557'、'71392'、-32835、'2016-06-03'、'16:03']的输出。
    现在的问题是它将行作为单独的simbol插入。这类:<代码>函数>函数获取测试1个测试1个测试1个数据。这类::<代码>函数获取函数测试1个测试1个测试1个数据。返回[返回[[2016-07-07-07-这类:::<代码>函数的函数。函数返回[返回[2016-07-07-07-07-7-7-08',[返回[[[2016-2007-2007-2007-7 7-7 7-7,'7139395,,-32835,-32835,,-32835,,[2016-2016-2016-2006-06-06-06-06-06-06-20-20-20-20-20-20-20-20-20-20-20-20-06-20-20-20-20-7,,,,,,[[2016-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10']、[“'”]、['7']、['1']、['3']、…..等等];}
    显然,它认为这就像是分离