反向切片Python(Slicing Python in Reverse)
在Python中我知道你可以将序列切片为seq [from:to + 1:step]。 但是如果你反向切片,使用-1的步长,为什么它不显示from元素? 它将一个元素从我期望的方向向右移动。
x="012345" x[4:2:-1]
打印43,而不是我预期的4321。 因此,如果step为-1,则打印seq [from:to-1:step]。 似乎不一致。
In Python I know you can slice sequences as seq[from:to+1:step]. But if you slice in reverse, using a step of -1, why does it not show the from element? It shifts one element to the right from what I would expect.
x="012345" x[4:2:-1]
prints 43, rather than 4321 as I expected. So if step is -1 then it prints seq[from:to-1:step]. Seems inconsistent.
原文:https://stackoverflow.com/questions/35248323
最满意答案
您可以使用以下异常处理程序
catch (WebException ex) { var response = ex.Response.GetResponseStream(); return response == null ? null : new StreamReader(response).ReadToEnd(); }
这将返回例如
{ "error": { "type": "invalid_request_error", "message": "Invalid API Key provided: ftw?!1" } }
You could use the following exception handler
catch (WebException ex) { var response = ex.Response.GetResponseStream(); return response == null ? null : new StreamReader(response).ReadToEnd(); }
This would return e.g.
{ "error": { "type": "invalid_request_error", "message": "Invalid API Key provided: ftw?!1" } }
相关问答
更多-
不要捕获一般Exception - 而是捕获可能包含Response的WebException 。 否则,只需在记录后重新抛出错误。 try { ... make request } catch (WebException webExcp) { Logger.Error(webExcp, "HttpRequest error: " + webExcp.Status); if (webExcp.Status == WebExceptionStatus.ProtocolError) { ...
-
您可能希望更改您的消费代码,以便为创建可以嘲笑包装实际实现的请求和响应的工厂接口。 更新:回顾 我的答复被接受后,我已经得到了低估,而且我承认我的原始答案质量差,并做出了很大的假设。 在4.5+以上模拟HttpWebRequest 我原来答案的混乱在于你可以在4.5中模拟HttpWebResponse ,但不是较早的版本。 在4.5中也可以使用过时的构造函数。 所以,推荐的行动方法是抽象请求和响应。 无论如何,下面是一个完整的工作测试,使用.NET 4.5与Moq 4.2。 [Test] public vo ...
-
编辑 :您是否尝试过一个简单的try-catch来查看是否可以获得更多详细信息? try { var response = (HttpWebResponse)(request.GetResponse()); } catch(Exception ex) { var response = (HttpWebResponse)ex.Response; } 在我的回答中,我注意到在代码中有一些关于编码的东西,你没有指定。 在这里查看例如这样的代码。 var encoding = ASCIIEncod ...
-
HttpWebResponse超时(HttpWebResponse timeout)[2022-07-23]
它正在等待这些标题 ..它有效 Uri u = new Uri("https://www.zomato.com/sk/brno/u-heligonky-z%C3%A1brdovice-brno-st%C5%99ed/denn%C3%A9-menu"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(u); request.AutomaticDecompression = DecompressionMeth ... -
首先,让我指出代码中的问题: using (StreamReader sr = new StreamReader(response.GetResponseStream())) { System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show(sr.ReadToEnd()); }); } 在您尝试显示结果时,流将关闭。 你应该做的是这样的事情: using (StreamReader sr = new ...
-
使用创建的HttpWebResponse接口返回401未授权错误(Using Created HttpWebResponse Interface Returns 401 Unauthorized Error)[2023-05-24]
你没有正确使用useDefaultCredentials ,试试这个: MyHttpWebRequest: public class MyHttpWebRequest : IHttpWebRequest { private readonly HttpWebRequest httpWebRequest; public MyHttpWebRequest(HttpWebRequest httpWebRequest) { this.httpWebRequest = http ... -
在wcf.codeplex.com上查看新的WCF Http堆栈。 目前你可以做类似的事情,在未来的版本中,他们正在计划一些事情来准确地解决你想做的事情。 您目前可以使用WCF Http堆栈执行以下操作。 public void DoSomething(HttpRequestMessage request, HttpResponseMessage response) { } Checkout the new WCF Http stack at wcf.codeplex.com. At the moment ...
-
您可以使用以下异常处理程序 catch (WebException ex) { var response = ex.Response.GetResponseStream(); return response == null ? null : new StreamReader(response).ReadToEnd(); } 这将返回例如 { "error": { "type": "invalid_request_error", "message": "Invalid A ...
-
处理来自HTTPWebRequest / HTTPWebResponse的错误(Handling Errors From HTTPWebRequest/HTTPWebResponse)[2023-03-11]
假设你有类似Hotmail>>login(user, password)东西Hotmail>>login(user, password)我肯定会使用异常。 对于您(以及您的域模型)而言,例外程度如何细致,并且很难实现平衡。 对于这种情况,我肯定会对最重要的事件(例如WrongCredentialsException )有异常,但我不会为每个4XX和5XX响应错误都有异常类。 但是,根据您的域名和个人喜好,您可能有一个ClientException和ServerException ,其中一个实例变量声明错误号 ... -
尝试 var jsondata = JsonConvert.DeserializeObject
- >(info);
这解决了该问题,因为例外情况表明: 要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”})或将反序列化类型更改为数组或实现集合接口的类型(例如ICollection,IList),例如List从JSON数组反序列化 重点强调突出重点 您收到的HTTP响应是一个对象数组,因此您需要以可以处理对象数组的方式对其进行反序列化。 通过 ...