Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用DataNitro,如何将python循环打印到excel,确保打印23行后,打印移动到相邻列?_Python_Excel_Datanitro - Fatal编程技术网

使用DataNitro,如何将python循环打印到excel,确保打印23行后,打印移动到相邻列?

使用DataNitro,如何将python循环打印到excel,确保打印23行后,打印移动到相邻列?,python,excel,datanitro,Python,Excel,Datanitro,我正在使用grequest从多个URL中提取json数据。在这里的另一位用户的帮助下,我能够让python连续打印每个url的结果。现在,我想使用DataNitro将这些结果打印到excel。这是我现在的代码 import grequests import json urls = [ 'https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-1ST&type=both&depth=50', 'ht

我正在使用grequest从多个URL中提取json数据。在这里的另一位用户的帮助下,我能够让python连续打印每个url的结果。现在,我想使用DataNitro将这些结果打印到excel。这是我现在的代码

import grequests
import json


urls = [
    'https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-1ST&type=both&depth=50',
    'https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-AMP&type=both&depth=50',
    'https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-ARDR&type=both&depth=50',
]

requests = (grequests.get(u) for u in urls)
responses = grequests.map(requests)

for response in responses:
    Cell("A1").value = "Buy"
    Cell("A2").value = (response.json()['result']['buy'][0]['Quantity'])
    Cell("A3").value = (response.json()['result']['buy'][0]['Rate'])
    Cell("A4").value = (response.json()['result']['buy'][1]['Quantity'])
    Cell("A5").value = (response.json()['result']['buy'][1]['Rate'])
    Cell("A6").value = (response.json()['result']['buy'][2]['Quantity'])
    Cell("A7").value = (response.json()['result']['buy'][2]['Rate'])
    Cell("A8").value = (response.json()['result']['buy'][3]['Quantity'])
    Cell("A9").value = (response.json()['result']['buy'][3]['Rate'])
    Cell("A10").value = (response.json()['result']['buy'][4]['Quantity'])
    Cell("A11").value = (response.json()['result']['buy'][4]['Rate'])
    Cell("A12").value = "Sell"
    Cell("A13").value = (response.json()['result']['sell'][0]['Quantity'])
    Cell("A14").value = (response.json()['result']['sell'][0]['Rate'])
    Cell("A15").value = (response.json()['result']['sell'][1]['Quantity'])
    Cell("A16").value = (response.json()['result']['sell'][1]['Rate'])
    Cell("A17").value = (response.json()['result']['sell'][2]['Quantity'])
    Cell("A18").value = (response.json()['result']['sell'][2]['Rate'])
    Cell("A19").value = (response.json()['result']['sell'][3]['Quantity'])
    Cell("A20").value = (response.json()['result']['sell'][3]['Rate'])
    Cell("A21").value = (response.json()['result']['sell'][4]['Quantity'])
    Cell("A22").value = (response.json()['result']['sell'][4]['Rate'])
    Cell("A23").value = "----"
这很好,但只有当我注释掉除一个url以外的所有url时,否则第一个url的结果会被第二个url的结果覆盖,这是预期的结果。然而,这不是我想要的。最后,我希望第一个url的结果打印在单元格A1:A23中,第二个url的结果打印在单元格B1:B23中,第三个url的结果打印在单元格C1:C23中

以下是我尝试过的:

for response in responses:
    #print("Buy")
    Quantity = [response.json()['result']['buy'][0]['Quantity'],
        response.json()['result']['buy'][1]['Quantity'],
        response.json()['result']['buy'][2]['Quantity'],
        response.json()['result']['buy'][3]['Quantity'],
        response.json()['result']['buy'][4]['Quantity']
]
    Cell("A1:E5").vertical = Quantity
(注意:我只是在这里试一下数量)

这不起作用,因为它只在列A中打印第一个url的结果,而不移动到列B以打印后续结果。我尝试切换
单元格(“A1:E5”)。垂直=数量
单元格(“A1:E5”)。垂直范围=数量
,但python返回以下错误:

    Cell("A1:E5").vertical_range = Quantity
  File "27\basic_io.py", line 379, in __setattr__
AttributeError: can't set attribute
我在考虑用我原来的设置做一些事情(例如,使用
单元格(“A2”).value=(response.json()['result']['buy'][0]['Quantity'])
类型的代码),添加类似
I=0
的内容,然后编写最后一行打印行来读取类似
单元格(23,(1+I)).value=“---”
但我(心里)想不出该怎么做。使用
单元格(23,(1+i)).value=“----”
行,它只会在单元格B1中打印“----”,然后它会再次覆盖A列中的所有结果,这在您看到代码实际执行的操作时也是有意义的

我不知所措,但我觉得答案相对简单,我只是错过了。我浏览了这个网站上提到DataNitro的9页结果:我找不到答案。我将感谢任何帮助

编辑:我意识到打印5然后移动到相邻列并不重要。我所需要做的就是将所有结果打印到excel,而不覆盖其本身:可以是所有结果显示在一列中,也可以是所有结果显示在一行中

我已经试过了

CellRange((1,1),(1,2)).value = [response.json()['result']['buy'][0]['Quantity'],
response.json()['result']['buy'][0]['Rate']
但我遇到了同样的问题,它只是覆盖了第一个url中的数据。提前谢谢

注释:问题(3)在重新运行脚本之前清除整个工作表的数据时得到解决

重新运行脚本将始终以
row=1
开始。如果要将数据追加到现有图纸集
行=旧数据的最大行+1


注释:此脚本将如何使用不同的JSON结构进行更改。将bittrex.com/api/v1.1/public/…与api.livecoin.net/exchange/order\u book?currencyPair=ABN/…进行比较

第一个是dict的
列表
,第二个是
列表
。使用
[0,1]
作为
键,例如:

# Usage for list
row += json_to_cell('Buy', response.json()['result']['buy'], row, [0, 1])

评论:一些。。。返回少于5个数据条目,或者有时一个URL根本不起作用

一般方法:

def json_to_cell(title, _json, start_row, keys):
    values = len(_json)
    if values:
        CellRange((start_row, 1), (start_row, 1)).value = [title]
        start_row += 1

        for row, key in enumerate(keys, start_row):
            rowData = []
            for c in range(0, values):
                rowData.append(_json[c][key])

            CellRange((row, 1), (row, values)).value = rowData
        return len(keys) + 1
    else:
        # Write some Info NO VALUES
        return 0

keys = ['Quantity', 'Rate']
row = 1
for response in responses:
    row += json_to_cell('Buy', response.json()['result']['buy'], row, keys)
    row += json_to_cell('Sell', response.json()['result']['sell'], row, keys)

    CellRange((row, 1), (row, 1)).value = ['----']
    row += 1

注释:索引器错误:CellRange设置为长度错误的对象

CellRange
len(rowData)
都应该是5。
添加一个
打印(…
),如下所示,并显示一个输出

使用以下命令:

for row, response in enumerate(responses, 1):
    rowData = []
    for c in range(0, 5):
        rowData.append(response.json()['result']['buy'][c]['Quantity'])

    print( rowData)
    CellRange((row, 1),(row, 5)).value = rowData

你为什么使用
.vertical
而不是
.value
?谢谢你的建议。我之所以使用vertical,是因为我在DataNitro文档中读到了一些东西,但我似乎再也找不到了。总之,当我运行
.value
而不是
.vertical
时,它确实会打印出响应,但会将它们全部放在一个单元格中并将它们括在括号中。如果我删除括号,它将再次开始覆盖值。表达式右侧的
Quantity
必须是值列表。我可以看到
print(Quantity)的输出吗
。我不确定这是否是你的意思,但当我运行
以获取响应时:打印(“购买”)数量=[response.json()['result']['Buy'][0]['Quantity'],response.json()['result']['Quantity'],response.json()['result']['Buy'][2]['Quantity'],response.json()['result']['Buy'][3] ['Quantity'],response.json()['result']['buy'][4]['Quantity']]print Quantity
I get
buy[1962.60059677,14.98071233,58.7580122416131.0,12.86986961]buy[27.583341942636.39735264108.5659107513262.620257271210.68142649]买入[11058.255774626000.0150.0139135.1851912261.404]
当我运行该命令时,会出现以下错误:
索引器:CellRange设置为长度错误的对象,但它会打印到列中。当我将
CellRange((行,1)、(行,5)).value=rowData
更改为
CellRange((行,1)、(行,15)).value=rowData
它确实会打印更多的列。运行后:
对于行,枚举中的响应(响应,1):rowData=[]对于范围(1,5)中的c:rowData.append(response.json()['result']['buy'][c]['Quantity'])打印(rowData单元格范围((行,1),(行,5))。value=rowData
我得到
[1006.14720103902.98186853,79.93646234,56.27735879]
如果这不是你的意思,很抱歉。@吉姆:我的错,我忽略了从
0开始,必须是
范围(0,5)
。输出中的一些问题:(1)它为每个url打印出5个以上的结果(它打印出每个“购买”数量和每个“购买”比率)(对于“出售”相同)(2)它打印出一对“数量”和“费率”,然后移动到下一列(这可能是有意的,但对我们来说是有效的,尽管它