我无法使切片正常工作。我有一个看起来像这样的字符串列表:
Dim jsonURL As String = "https://prod-13.canadacentral.logic.azure.com******************************************"
Dim reader As StreamReader
Dim errorMsg As String = Nothing
Dim CurrentFurn As String
Try
Dim request As HttpWebRequest = CType(WebRequest.Create(jsonURL), HttpWebRequest)
request.Credentials = New NetworkCredential(USER,PASSWORD")
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Dim jsonStr As String = reader.ReadToEnd()
Dim TCICFurnaceMiner = JsonConvert.DeserializeObject(Of List(Of JSON_data))(jsonStr)
If CurrentMachine = "F1" Then
CurrentFurn = "Furnace 1 (lbs per hour 2400)"
ElseIf CurrentMachine = "F2" Then
CurrentFurn = "Furnace 2 (lbs per hour 4200)"
End If
Dim ProdList = From ProdData In TCICFurnaceMiner Where ProdData.Machine.Contains(CurrentFurn) Select ProdData
chrtFurnace.ChartAreas(0).AxisX.LabelStyle.Angle = -45
chrtFurnace.ChartAreas(0).AxisX.Interval = 1
chrtFurnace.ChartAreas(0).AxisX.Minimum = 0
chrtFurnace.ChartAreas(0).AxisX.Maximum = 24.5
' chrtFurnace.ChartAreas(0).AxisX.LabelStyle.Format = "HH:00"
chrtFurnace.ChartAreas(0).AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Auto
chrtFurnace.ChartAreas(0).AxisX.Title = "Hour"
chrtFurnace.ChartAreas(0).AxisY.Title = "Lbs"
chrtFurnace.ChartAreas(0).AxisX.MajorGrid.Enabled = False
chrtFurnace.ChartAreas(0).AxisY.MajorGrid.Enabled = False
chrtFurnace.ChartAreas(0).AxisX.TitleFont = New Drawing.Font("arial", 16)
chrtFurnace.ChartAreas(0).AxisY.TitleFont = New Drawing.Font("arial", 16)
chrtFurnace.Series("Lbs").IsValueShownAsLabel = True
' Print the results of our LINQ query
For Each ProdData In ProdList
chrtFurnace.Series("Lbs").Points.Add(ProdData.Lbs)
chrtFurnace.Series("Rate").Points.Add(ProdData.Rate)
Next
Catch ex As WebException
errorMsg = "Download failed. The response from the server was: " +
CType(ex.Response, HttpWebResponse).StatusDescription
Console.WriteLine("Error: " + errorMsg)
End Try
我需要将'url'保存在新列表中以进一步处理它。
这就是我正在尝试的
['subdomain', 'name', 'url']
['https://www.pedidosya.com.ar/restaurantes/buenos-aires/recoleta/empanadas-delivery?bt=RESTAURANT&page=1', 'Cümen-Cümen Empanadas Palermo', 'https://www.pedidosya.com.ar/restaurantes/buenos-aires/cumen-cumen-empanadas-palermo-menu']
['https://www.pedidosya.com.ar/restaurantes/buenos-aires/recoleta/empanadas-delivery?bt=RESTAURANT&page=1', 'Cümen-Cümen Empanadas - Barrio Norte', 'https://www.pedidosya.com.ar/restaurantes/buenos-aires/cumen-cumen-empanadas-barrio-norte-menu']
但这是我打印()时得到的:
for row[3:3] in reader:
menus = []
menus.append[row]
哪个是列表的最后一部分。 我需要的是:
['https://www.pedidosya.com.ar/restaurantes/buenos-aires/recoleta/empanadas-delivery?bt=RESTAURANT&page=5', 'La Pergola - Recoleta', 'https://www.pedidosya.com.ar/restaurantes/buenos-aires/la-pergola-recoleta-menu']
我已经添加了其余的代码。问题是,这不是我想像的str列表,而是type()='_csv.reader'
这是完整的代码:
menus = ['https://www.pedidosya.com.ar/restaurantes/buenos-aires/cumen-cumen-empanadas-palermo-menu', 'https://www.pedidosya.com.ar/restaurantes/buenos-aires/cumen-cumen-empanadas-barrio-norte-menu']
答案 0 :(得分:1)
似乎想要这样:
TypeError: a bytes-like object is required, not 'str'
我不理解通过使menus = []
for row in reader:
menus.append(row[2])
循环的迭代变量row[3:3]
来做什么。我认为您想遍历简单的行,然后对循环中的每一行进行一些操作。
答案 1 :(得分:1)
假设您有一个列表列表(即列表周围有一个额外的[]
),而不是问题所暗示的3个孤立列表,则可以遍历列表列表并使用url
元素从每个(元素2)添加到新列表。
reader = csvreader or whatever you do to define it
menu = []
for n, i in enumerate(reader):
if(n != 0):
print(i[2])
menu.append(i[2])
我更改了代码以与csvreader对象一起使用。我们将使用enumerate
一个出色的函数来代替我们忽略第一个元素的旧方法,该函数将我们属于阅读器的哪个元素视为n
。因此,只要n
不为零,我们将像以前一样继续。
答案 2 :(得分:0)
问题不在于切片(尽管您也可以直接用[2]进行索引)。但是,您在循环中重新初始化菜单。因此,对于循环的每次运行,您都将覆盖之前的内容。 这应该解决它:
menus = []
for row in reader:
menus.append[row[2]]
一种更清洁(并且更具Pythonic性)的方法是使用list comprehension:
menus = [row[2] for row in reader]