从python中的txt文件中提取字符之间的字符串 [英] Extract string between characters from a txt file in python

查看:4340
本文介绍了从python中的txt文件中提取字符之间的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个txt文件,我想让python读取,并从中我希望python提取一个字符串,具体在两个字符之间。例如:



换行



换行b



$ c







$ p $&$ $ $ $ $ $ $ $ $ $ $ $

Line e



我想要的是python读取行,当遇到&



有没有建议?


我想要它开始打印行(包括带有$的行)

解决方案

这样做:

  data = [] 
flag = False
with open('/ tmp / test.txt','r')as f:
for line in f:
if line.startswith('& ):
flag = True
如果标志:
data.append(line)
如果line.strip()。endswith('!'):
flag = False

print''.join(data)

小到足以将它读入内存不是一个问题,并且在& 中没有歧义你想要的字符串的开始和结束,这是更容易:

  with open('/ tmp / test.txt' 'r')as f:
data ='。join(f.readlines())

打印数据[data.index('&'):data.index !')+ 1]

或者,如果要读取整个文件, code>& 和如果它们分别在行的开头和结尾,则可以使用regex: / p>

  import re 

with open('/ tmp / test.txt','r')as f:
data ='。join(f.readlines())

m = re.search(r'^(&。*!)\s *?\\\
',data,re.S | re.M)
如果m:print m.group(1)


I have a txt file that I want python to read, and from which I want python to extract a string specifically between two characters. Here is an example:

Line a

Line b

Line c

&TESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTEST !

Line d

Line e

What I want is python to read the lines and when it encounters "&" I want it to start printing the lines (including the line with "$") up untill it encounters "!"

Any suggestions?

解决方案

This works:

data=[]
flag=False
with open('/tmp/test.txt','r') as f:
    for line in f:
        if line.startswith('&'):
            flag=True
        if flag:
            data.append(line)
        if line.strip().endswith('!'):
            flag=False

print ''.join(data)  

If you file is small enough that reading it all into memory is not an issue, and there is no ambiguity in & or ! as the start and end of the string you want, this is easier:

with open('/tmp/test.txt','r') as f:
    data=''.join(f.readlines())    

print data[data.index('&'):data.index('!')+1] 

Or, if you want to read the whole file in but only use & and ! if they are are at the beginning and end of the lines respectively, you can use a regex:

import re

with open('/tmp/test.txt','r') as f:
    data=''.join(f.readlines())    

m=re.search(r'^(&.*!)\s*?\n',data,re.S | re.M)    
if m: print m.group(1)   

这篇关于从python中的txt文件中提取字符之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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