【问题标题】:Fixing Incorrect Python Array Format修复不正确的 Python 数组格式
【发布时间】:2020-12-01 03:49:28
【问题描述】:

由于某种原因,我无法打印整个数组或以正确的格式循环。

数组中的每个条目都应该是一个单独的 URL。

import requests
import json

response = requests.get('https://newtrackon.com/api/live') 

#print(response.text)

my_input = [] 
my_input.append(response.text) 
#print(my_input) 

x = my_input[0]

print(x)

#for a in x:
#    print(a)

【问题讨论】:

  • 您需要将 URL 中的字符串拆分为列表的元素。

标签: python arrays python-3.x list arraylist


【解决方案1】:

您需要将 URL 中的字符串拆分为列表的元素。

import requests
import json

response = requests.get('https://newtrackon.com/api/live') 

my_input = response.text.split("\n")

print(len(my_input))

# Remove the blank rows
my_input = list(filter(None, my_input))

print(len(my_input))

【讨论】:

  • 如果这解决了您的问题,请接受解决方案。
【解决方案2】:

替换

    my_input = []
    my_input.append(response.text)

    my_input = response.text.split("\n\n")

response.text 将所有 url 作为单个字符串返回

【讨论】:

  • 用两个换行符分割是一个有效的解决方案。请注意,第一个任务什么也没做:my_input.append(response.text)
猜你喜欢
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 1970-01-01
相关资源
最近更新 更多