首页 \ 问答 \ 反向切片Python(Slicing Python in Reverse)

反向切片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
更新时间:2023-10-16 15:10

最满意答案

您可以使用以下异常处理程序

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 ...
  • 它正在等待这些标题 ..它有效 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 ...
  • 你没有正确使用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 ...
  • 假设你有类似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响应是一个对象数组,因此您需要以可以处理对象数组的方式对其进行反序列化。 通过 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。