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

如何在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?

相关问答

更多
  • 现在可以在AppHarbor上运行Machine.Specifications(MSpec)测试。 It is now possible to run Machine.Specifications (MSpec) tests on AppHarbor.
  • 好吧,我不确定问题是什么,但我找到了解决办法...... 我现在使用以下代码,它似乎工作: StreamReader reader = new StreamReader(stream); String res = reader.ReadToEnd(); NameValueCollection coll = HttpUtility.ParseQueryString(res); criteria.Retailers= coll["Retailers"].Split('|'); 我在这篇文章中找到了答案: WC ...
  • 如果您想在其中一个提到的托管商处拥有私人帐户,您唯一的选择就是Bitbucket 。 他们的免费计划包括无限制的私人Git和Mercurial存储库(唯一的限制是不超过五个用户,包括您)可以访问私有存储库。 GitHub也提供私有存储库, 但不是免费的 。 CodePlex完全免费, 但只允许开源项目 (没有私人项目)。 编辑: 使用“集成Appharbor工具到NuGet”,你的意思是AppHarbor拉动NuGet包,这样你就不必将它们提交到你的存储库了吗? 如果是,请查看使用NuGet包还原以避免将包 ...
  • 所以,我的代码中有一个错误,但下面的信息是我用来了解AppHarbor正在对配置变量做什么,并允许我修复它。 本文包含有关配置变量如何工作的模糊细节,但这里是TLDR。 配置变量仅适用于AppSettings; 你不能将它们与连接字符串一起使用 AppSettings键值必须与配置变量的名称匹配。 Web配置转换发生在配置变量更新之前。 FINAL步骤是使用配置变量更新AppSettings。 来自AppHarbor支持的说明: 在实际部署应用程序之前,不会注入应用程序的生产配置,因此如果从构建日志页面下载 ...
  • 我解决了这个问题 - 发生这种情况的原因是因为内容未被复制到输出目录。 我不确切知道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 dire ...
  • 我必须将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 seem ...
  • 事实证明, _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 was s ...
  • 您是否尝试过按照此问题中的建议将“ Generate serialization assembly ”设置为“关闭”? Have you tried setting Generate serialization assembly to "Off" as suggested in this question?
  • 通过AppHarbor配置附加组件为您提供了自动应用程序配置,整合计费以及能够管理AppHarbor中所有内容的优势(无需记住X登录,也无需记住在各种服务提供商处更新X信用卡)。 我们试图让标题尽可能不显眼,并且它似乎在我们的大多数附加合作伙伴网站上运行良好。 如果它导致任何地方破损,请给我们留言。 我们还不断与我们的附加合作伙伴合作,使他们的附加计划产品保持最新状态,我刚刚给AirBrake家伙发了一封电子邮件。 感谢您提醒我们这个问题! Provisioning add-ons through AppH ...
  • 将HintPath更改为..\packages对我来说不起作用,但将其更改为$(Solutiondir)\packages对我来说就像魅力一样。 There were some paths in the csproj that were broken: ..\web\packages\mongocsharpdriver.1.8.3\lib\net35\MongoDB.Bs ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)