标题:请教一下python提取相应的内容问题
只看楼主
shansh
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-11-27
结帖率:100%
已结贴  问题点数:20 回复次数:7 
请教一下python提取相应的内容问题
各位大神,想请教一下,我想把下面的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)
搜索更多相关主题的帖子: python 内容 line 提取 date 
2019-11-27 14:44
fall_bernana
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:17
帖 子:240
专家分:2086
注 册:2019-8-16
得分:20 
以下是引用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编辑过]

2019-11-27 15:44
shansh
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-11-27
得分:0 
回复 2楼 fall_bernana
非常谢谢,解决了我很久的问题,我是刚刚学习python。谢谢!
2019-11-27 20:39
shansh
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-11-27
得分:0 
回复 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)
2019-12-04 16:36
fall_bernana
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:17
帖 子:240
专家分:2086
注 册:2019-8-16
得分:0 
以下是引用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 .在循环里他是不变的空值.
2019-12-04 17:00
shansh
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-11-27
得分:0 
回复 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)
2019-12-04 18:02
shansh
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-11-27
得分:0 
回复 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
2019-12-04 20:11
bczgsky
Rank: 2
等 级:论坛游民
威 望:1
帖 子:10
专家分:20
注 册:2019-12-5
得分:0 
看来这个论坛热心人也不少,以前怎么没听说呢
2019-12-06 15:57



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-498110-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.064454 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved