如何在Python中访问文件(.txt)中的下一行(How to access next line in a file(.txt) in Python)

我有一个文件然后打开它然后检查一些东西。 如果该行以“E PARAM”开头并且其中也有“OOPS1”,那么我想检查下一行是否以“E PARAM”开头。 如果不是,我会创建一个新文件并将其复制到那里,直到我没有打到另一个“E PARAM”行。 由于Python没有next()选项......什么可以帮助我

import string
import os


A = "k_trap_cur"
B = open(A, 'r+')
lines = B.readline()

for lines in B:
    if lines.startswith("E PRAM"):
        if "OOPS: 1" in lines:
            while lines.next().startswith("E PARAM") == False: // HERE I want to access next line
                print " YES"

I have a file and I open it and then I check for few things. If the line starts with "E PARAM" and also has "OOPS1" in it somewhere then I want to check that the next line starts with " E PARAM". If not I will make a new file and copy it there till the time I dont hit another "E PARAM" line. Since Python doesnt have a next() option... what can help me here

import string
import os


A = "k_trap_cur"
B = open(A, 'r+')
lines = B.readline()

for lines in B:
    if lines.startswith("E PRAM"):
        if "OOPS: 1" in lines:
            while lines.next().startswith("E PARAM") == False: // HERE I want to access next line
                print " YES"

原文:https://stackoverflow.com/questions/17075901
2022-07-22 18:07

满意答案

好吧,我不确定问题是什么,但我找到了解决办法......

我现在使用以下代码,它似乎工作:

StreamReader reader = new StreamReader(stream);
String res = reader.ReadToEnd();
NameValueCollection coll = HttpUtility.ParseQueryString(res);
criteria.Retailers= coll["Retailers"].Split('|');

我在这篇文章中找到了答案: WCF + REST:请求数据在哪里?


Well I'm not sure what the problem is, but I found a work-around...

I now use the following code and it seems to work:

StreamReader reader = new StreamReader(stream);
String res = reader.ReadToEnd();
NameValueCollection coll = HttpUtility.ParseQueryString(res);
criteria.Retailers= coll["Retailers"].Split('|');

I found the answer on this post: WCF + REST: Where is the request data?

相关问答

更多

MSpec在Appharbor上(MSpec on Appharbor)

现在可以在AppHarbor上运行Machine.Specifications(MSpec)测试。 It is now possible to run Machine.Specifications (MSpec) tests on AppHarbor.

部署到AppHarbor后,WebInvoke表单元素为空(WebInvoke Form elements are null after deploying to AppHarbor)

好吧,我不确定问题是什么,但我找到了解决办法...... 我现在使用以下代码,它似乎工作: StreamReader reader = new StreamReader(stream); String res = reader.ReadToEnd(); NameValueCollection coll = HttpUtility.ParseQueryString(res); criteria.Retailers= coll["Retailers"].Split('|'); 我在这篇文章中找到了答...

使用subversion部署到AppHarbor(deploying to AppHarbor using subversion)

如果您想在其中一个提到的托管商处拥有私人帐户,您唯一的选择就是Bitbucket 。 他们的免费计划包括无限制的私人Git和Mercurial存储库(唯一的限制是不超过五个用户,包括您)可以访问私有存储库。 GitHub也提供私有存储库, 但不是免费的 。 CodePlex完全免费, 但只允许开源项目 (没有私人项目)。 编辑: 使用“集成Appharbor工具到NuGet”,你的意思是AppHarbor拉动NuGet包,这样你就不必将它们提交到你的存储库了吗? 如果是,请查看使用NuGet包还原...

AppHarbor配置变量未更新(AppHarbor Configuration Variables Not Updating)

所以,我的代码中有一个错误,但下面的信息是我用来了解AppHarbor正在对配置变量做什么,并允许我修复它。 本文包含有关配置变量如何工作的模糊细节,但这里是TLDR。 配置变量仅适用于AppSettings; 你不能将它们与连接字符串一起使用 AppSettings键值必须与配置变量的名称匹配。 Web配置转换发生在配置变量更新之前。 FINAL步骤是使用配置变量更新AppSettings。 来自AppHarbor支持的说明: 在实际部署应用程序之前,不会注入应用程序的生产配置,因此如果从构建日...

appharbor没有部署静态内容(appharbor not deploying static content)

我解决了这个问题 - 发生这种情况的原因是因为内容未被复制到输出目录。 我不确切知道AppHarbor如何在那里构建部署脚本,但我将Copy To Output Directory选项值更改为Copy Always ,这解决了问题。 ( Build Action已设置为Content ) I solved the problem - The reason this was happening because the content wasn't being copied to the output...

在Appharbor上无头(Dotless on Appharbor)

我必须将Build Action值设置为Content ,然后将Copy To Output DIrectory值设置为Copy always 。 这样做后,它似乎正常工作! WOOHOOOO! 这太令人兴奋了。 这是过去几天的重大问题。 I had to set the Build Action value to Content and then set the Copy To Output DIrectory value to Copy always. After doing that, it...

AppHarbor上的布局被忽略(Layout being ignored on AppHarbor)

事实证明, _ViewStart.cshtml文件不再是项目的一部分。 由于物理文件仍然存在,本地调试正在捡起它并且运行正常。 但是,如果它不是构建的一部分,当AppHarbor构建项目时,它不包括导致所有视图(没有显式布局)使用Layout = null进行渲染 感谢大家! 捂脸 So it turns out that the _ViewStart.cshtml file was no longer a part of the project. Since the physical file ...

在AppHarbor上部署抛出混合模式程序集错误(Deploying on AppHarbor throwing Mixed Mode Assembly error)

您是否尝试过按照此问题中的建议将“ Generate serialization assembly ”设置为“关闭”? Have you tried setting Generate serialization assembly to "Off" as suggested in this question?

为什么要使用AppHarbor插件?(Why use AppHarbor addons?)

通过AppHarbor配置附加组件为您提供了自动应用程序配置,整合计费以及能够管理AppHarbor中所有内容的优势(无需记住X登录,也无需记住在各种服务提供商处更新X信用卡)。 我们试图让标题尽可能不显眼,并且它似乎在我们的大多数附加合作伙伴网站上运行良好。 如果它导致任何地方破损,请给我们留言。 我们还不断与我们的附加合作伙伴合作,使他们的附加计划产品保持最新状态,我刚刚给AirBrake家伙发了一封电子邮件。 感谢您提醒我们这个问题! Provisioning add-ons through...

AppHarbor使用nuget包构建错误(AppHarbor build error with nuget packages)

将HintPath更改为..\packages对我来说不起作用,但将其更改为$(Solutiondir)\packages对我来说就像魅力一样。 There were some paths in the csproj that were broken: <ItemGroup> <Reference Include="MongoDB.Bson"> <HintPath>..\web\packages\mongocsharpdriver.1.8.3\lib\net35\Mongo...

相关文章

更多

Securing Solr on Tomcat access using a user account

Open [Tomcat install dir]\tomcat-users.xmlfor editi ...

探索 Python,第 1 部分: Python 的内置数值类型

Python 编程语言具有很高的灵活性,它支持多种编程方法,包括过程化的、面向对象的和函数式的。但最重 ...

ACCESS TOKEN

Access Token 在微信公众平台接口开发中,Access Token占据了一个很重要的地位,相 ...

python2和python3的区别

python2和python3的区别,1.性能 Py3.0运行 pystone benchmark的速 ...

Python资源索引 【转载】

原文地址:http://blog.chinaunix.net/uid-25525723-id-3630 ...

Python的文件类型

Python的文件类型 Python有三种文件类型,分别是源代码文件、字节码文件和优化代码文件 源代 ...

Becoming a data scientist

Data Week: Becoming a data scientist Data Pointed, ...

采用Python来访问Hadoop HSFS存储实现文件的操作

用python访问hdfs是个很头疼的事情。 这个是pyhdfs的库 import pyhdfs f ...

Python 在Linux中使用SQLite3

如果出现如下错误: &gt;&gt;&gt; import sqlite3 Traceback (mo ...

Python内建函数(A)

abs(x) 说明:abs(x)返回x的绝对值,如果参数是复数,则返回复数的模; 参数x:整 ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本