编辑文本文件 [英] editing a text file

查看:64
本文介绍了编辑文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我也很喜欢python表单和编程


我有一个文本文件,我需要提取几个单词标题中的数据(由3行组成)并在整个文件(由150行组成的文件)中搜索与特定id相关的关键字TEXT1,TEXT2,TEXT3并得到TEXT1,TEXT2的WRITES的总和,TEXT3


ex:输入文件将如下


Windows 2000文本文本文本文本文本文本
343434文本文本文本文本文本文本代码textplayback

文本文本文本文本文本开始检查文本..


文本文本文本文本文本343434 ,文字ID:504181,文件:text1,Write_0 = 24

文本文本文本文本文本343434,文本ID:504182,文件:text1,Write_0 = 20

文本文本文本文本文本343434,文本ID:504183,文件:text1,Write_0 = 21

文本文本文本文本文本343434,文本ID:504181,文件:text2,Write_0 = 29

文本文本文本文本343434,文本ID:504182,文件:text2,Write_0 = 20

文本文本文本文本文本343434,文本ID:504183,文件:text2,Write_0 = 23

text text text text 343434,text ID:504181,File:text3,Write_0 = 21

text text text text 343434,text ID:504182,File:text3,Write_0 = 24

文本文本文本文本文本343434,文本ID:504183,文件:text3,Write_0 = 23

输出应该如下:


343434 textplayback,开始检查 - >这是来自标题

文本ID:504181:(24 + 21 + 29)之和即27 ---->这是整个文件搜索的结果


请帮助我这方面

提前感谢您的帮助

Hi there,

Iam new to python forms and programming too

I had a text file where i need to extract few words of data from the header(which is of 3 lines) and search for the keyword TEXT1, TEXT2, TEXT3in entire file(file consisting of 150 lines) that is related to a particular id and get the sum of the, WRITES of TEXT1, TEXT2, TEXT3

ex: input file will be as below


Windows 2000 text text text text text text text
343434 text text text text text text code textplayback
text text text text text start check text..

text text text text text 343434, text ID: 504181, File: text1, Write_0=24
text text text text text 343434, text ID: 504182, File: text1, Write_0=20
text text text text text 343434, text ID: 504183, File: text1, Write_0=21
text text text text text 343434, text ID: 504181, File: text2, Write_0=29
text text text text text 343434, text ID: 504182, File: text2, Write_0=20
text text text text text 343434, text ID: 504183, File: text2, Write_0=23
text text text text text 343434, text ID: 504181, File: text3, Write_0=21
text text text text text 343434, text ID: 504182, File: text3, Write_0=24
text text text text text 343434, text ID: 504183, File: text3, Write_0=23

output should be like:

343434 textplayback, start check-->this is from the header
text ID: 504181: sum of (24+21+29) ie 27----> this is result of entire file search

Please help me in this regard
Thanks in advance for your help

推荐答案

你好,


i需要帮助才能获得文本文件所需的内容


如何修改文本文件,通过过滤掉不需要的文本


ex:输入文件将如下所示


Windows 2000文本文本文本文本文本文本文本

343434文本文本文本文本文本文本代码textplayback

文本文本文本文本文本开始检查文本..


text text text text 343434,text ID:504181,File:text1,Write_0 = 24

text text text text 343434,text ID:504182,File:text1,Write_0 = 20

text text text text 343434,text ID:504183,File:text1,Write_0 = 21

text text text text 343434,text ID:504181,File:text2,Write_0 = 29

文本文本文本文本文本343434,文本ID:504 182,File:text2,Write_0 = 20

text text text text 343434,text ID:504183,File:text2,Write_0 = 23

text text text text 343434 ,文字ID:504181,文件:text3,Write_0 = 21

文本文本文本文本文本343434,文本ID:504182,文件:text3,Write_0 = 24

文本文本文本文本文本343434,文本ID:504183,文件:text3,Write_0 = 23

输出应该如下:


i需要获得第1名和第二行避免第三行像标题一样


以及整个文件中文本id504181和文件text1的写入时间总和


如何搜索它们。请帮助我这方面

非常感谢
Hi there,

i need help in getting the needed contents of the text file

how can i modify the text file, by filtering out the unwanted text

ex: input file will be as below


Windows 2000 text text text text text text text
343434 text text text text text text code textplayback
text text text text text start check text..

text text text text text 343434, text ID: 504181, File: text1, Write_0=24
text text text text text 343434, text ID: 504182, File: text1, Write_0=20
text text text text text 343434, text ID: 504183, File: text1, Write_0=21
text text text text text 343434, text ID: 504181, File: text2, Write_0=29
text text text text text 343434, text ID: 504182, File: text2, Write_0=20
text text text text text 343434, text ID: 504183, File: text2, Write_0=23
text text text text text 343434, text ID: 504181, File: text3, Write_0=21
text text text text text 343434, text ID: 504182, File: text3, Write_0=24
text text text text text 343434, text ID: 504183, File: text3, Write_0=23

output should be like:

i need to get 1st and 2nd line avoiding the third like in the header

and also sum of the write times for the text id504181 and file text1 from the entire file

How can i search for them. Please help me in this regard
Thanks so much


我没有太多时间思考整个过程,但似乎有些基本概念会有所帮助。这是我刚刚完成的一个会话,并评论说明了如何访问某些值。你会发现python通常有很多不同的方法可以实现一个给定的方法,并且总是有一种更紧凑的方式(除非你从这里的几个真正的专业人士那里得到它)。这些只是想法。

I haven''t had a lot of time to think through the entire process, but it seems like some basic concepts would be helpful. Here is a session I just ran through and commented that shows how some of the values could be accessed. You will find with python that there are generally dozens of different ways to accomplish a given and there is always a more compact way (unless you get it from a couple of the real pros here). These are just ideas.

展开 | 选择 | Wrap | 行号


bluemountain,


希望dshimer的建议能让您找到解决方案。以下使用 re 模块。
bluemountain,

Hopefully dshimer''s suggestions will enable you to find a solution. The following makes use of the re module.
展开 | 选择 | Wrap | 行号


这篇关于编辑文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆