注册 登录
编程论坛 Python论坛

请教一下python提取相应的内容问题

shansh 发布于 2019-11-27 14:44, 2275 次点击
各位大神,想请教一下,我想把下面的txt内容进行提取并且保存到另外一个txt文档中,应该如何操作,使用的是python3
--------------------------A-------------------------
1
2
3
4
5
--------------------------B-------------------------
使用以下的代码,可以提取到内容,但是结果是list,并且无法进行换行,请问还有其它好的方法吗,谢谢!
import re
date = []
recording = False
with open('data/2.txt','r') as filename:
    for line in filename:
        line = line.strip()
        
        if line == '--------------------------B--------------------------':
            break

        if recording:
            date.append(line)

        if line == '--------------------------A-------------------------':
            recording = True
            filename.write
             '\n'.join(date)

print(date)
7 回复
#2
fall_bernana2019-11-27 15:44
以下是引用shansh在2019-11-27 14:44:50的发言:

各位大神,想请教一下,我想把下面的txt内容进行提取并且保存到另外一个txt文档中,应该如何操作,使用的是python3
--------------------------A-------------------------
1
2
3
4
5
--------------------------B-------------------------
使用以下的代码,可以提取到内容,但是结果是list,并且无法进行换行,请问还有其它好的方法吗,谢谢!
import re
date = []
recording = False
with open('data/2.txt','r') as filename:
    for line in filename:
        line = line.strip()
        
        if line == '--------------------------B--------------------------':
            break

        if recording:
            date.append(line)

        if line == '--------------------------A-------------------------':
            recording = True
            filename.write
             '\n'.join(date)

print(date)

程序代码:
import re
date = []
recording = False
with open('data/2.txt','r') as filename:
    lines=filename.readlines()
    with open('data/3.txt','w') as fileout:
        for line in lines:
            line = line.strip()
            
            if line == '--------------------------B--------------------------':
                break

            if recording:
                date.append(line)
                fileout.write(line+"\n")

            if line == '--------------------------A-------------------------':
                recording = True
print(date)


[此贴子已经被作者于2019-11-27 15:45编辑过]

#3
shansh2019-11-27 20:39
回复 2楼 fall_bernana
非常谢谢,解决了我很久的问题,我是刚刚学习python。谢谢!
#4
shansh2019-12-04 16:36
回复 2楼 fall_bernana
您好!我还能不能请教一下。在匹配'--------------------------B--------------------------'这个时我使用re.findall没有办法进行正则表达式的匹配,我尝试使用b来验证一下,还是没有办法来匹配到。请问我是那里有错吗?

import re
print('输入设备名称:',end='')
devicename = input()
devicenametemp = 'tempdata/' + devicename + '_CatalystAndAsr_version' + '.txt'
devicename = 'data/'+ devicename + '.txt'

date = []
recording = False
with open(devicename,'r') as filename:
     lines=filename.readlines()
     devicefile = filename.read()
     
   
     with open(devicenametemp,'w') as fileout:
        for line in lines:
            line = line.strip()
            b =re.findall('.*B.*',devicefile)
            if line == re.findall('.*B.*',line) :
                break

            if recording:
                date.append(line)
                fileout.write(line+"\n")

            if line == re.findall('.*A.*',line):
                recording = True
print(b)
#5
fall_bernana2019-12-04 17:00
以下是引用shansh在2019-12-4 16:36:18的发言:

您好!我还能不能请教一下。在匹配'--------------------------B--------------------------'这个时我使用re.findall没有办法进行正则表达式的匹配,我尝试使用b来验证一下,还是没有办法来匹配到。请问我是那里有错吗?

import re
print('输入设备名称:',end='')
devicename = input()
devicenametemp = 'tempdata/' + devicename + '_CatalystAndAsr_version' + '.txt'
devicename = 'data/'+ devicename + '.txt'

date = []
recording = False
with open(devicename,'r') as filename:
     lines=filename.readlines()
     devicefile = filename.read()
     
   
     with open(devicenametemp,'w') as fileout:
        for line in lines:
            line = line.strip()
            b =re.findall('.*B.*',devicefile)
            if line == re.findall('.*B.*',line) :
                break

            if recording:
                date.append(line)
                fileout.write(line+"\n")

            if line == re.findall('.*A.*',line):
                recording = True
print(b)

lines=filename.readlines()
devicefile = filename.read()
readlines()会把所有内容存放到lines里,同时filename指针会指向文件最后,这时你再用filename.read()读取的只会是空.
你直接使用line就可以b =re.findall('.*B.*',line);为啥要用 devicefile .在循环里他是不变的空值.
#6
shansh2019-12-04 18:02
回复 5楼 fall_bernana
非常高兴能看到您的回复,我使用您提供的代码运行是正常的,如果我把 if line == '--------------------------B--------------------------':使用re.findall来替换,发现是不行,经过刚刚的测试,我发现是不是通过re.findall来提取结果的是list,而if需要匹配的str,导致if无法生效。谢谢!
import re
date = []
recording = False
with open('data/2.txt','r') as filename:
    lines=filename.readlines()
    with open('data/3.txt','w') as fileout:
        for line in lines:
            line = line.strip()
            
            if line == '--------------------------B--------------------------':
                break

            if recording:
                date.append(line)
                fileout.write(line+"\n")

            if line == '--------------------------A-------------------------':
                recording = True
print(date)

替换如下
import re
date = []
recording = False
with open('data/2.txt','r') as filename:
    lines=filename.readlines()
    with open('data/3.txt','w') as fileout:
        for line in lines:
            line = line.strip()
            
            if line ==  re.findall('.*B.*',line) :
                break

            if recording:
                date.append(line)
                fileout.write(line+"\n")

            if line == re.findall('.*A.*',line):
                recording = True
print(date)
#7
shansh2019-12-04 20:11
回复 6楼 shansh
又麻烦您了,我刚刚测试了一下,我把list转为str就可以,但是遇到一个问题的是当4和5中间有个空的换行时,只能列出4的数据。无法列出5,可能是我的表达式有问题,我需要再研究一下,谢谢!
-------------------------A-------------------------
1
2
3
4

5
--------------------------B-------------------------



date = []
recording = False
with open(devicename,'r') as filename:
     lines=filename.readlines()
     
     with open(devicenametemp,'w') as fileout:
        for line in lines:
            line = line.strip()
            
           
            if line == (''.join(re.findall('.*B.*',line))):
                print(line)
                break

            if recording:
                date.append(line)
                fileout.write(line+"\n")

            if line == (''.join(re.findall('.*A.*',line))):
               
                recording = True
#8
bczgsky2019-12-06 15:57
看来这个论坛热心人也不少,以前怎么没听说呢
1