Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从文本文件中筛选行_Python - Fatal编程技术网

Python 从文本文件中筛选行

Python 从文本文件中筛选行,python,Python,我必须清除一个txt文件(test.txt),删除以“(”或空行开头的行,然后重命名它(test_ok.txt) 我使用此功能: def clearfile(filename): for row in (open (filename, 'r')).readlines(): if row[0][0]<>'\n' and riga[0][0]<>'(' : out_file = open('%s_%s%s' %(os.path.

我必须清除一个txt文件(test.txt),删除以“(”或空行开头的行,然后重命名它(test_ok.txt)

我使用此功能:

def clearfile(filename):
    for row in (open (filename, 'r')).readlines():
        if row[0][0]<>'\n' and riga[0][0]<>'(' :
            out_file = open('%s_%s%s' %(os.path.splitext(filename)[0],'ok',os.path.splitext(os.path.basename(filename))[1]) , 'a')
            out_file.write(riga) 
            out_file.close()
    return out_file
def clearfile(filename):
    with open (filename, 'r') as a, open('%s_%s%s' %(os.path.splitext(filename)[0],'ok',os.path.splitext(os.path.basename(filename))[1]) , 'a') as b:
        for row in (a).readlines():
            if row[0][0]!='\n' and row[0][0]!= '(' :
                b.write(row) 
        b.close

为什么要多次打开/关闭文件

def clearfile(filename):
    out_file = open('%s_%s%s' %(os.path.splitext(filename)[0], 'ok', os.path.splitext(os.path.basename(filename))[1]) , 'a')
    for row in (open (filename, 'r')).readlines():
        if row[0][0]!= '\n' and riga[0][0]!='(' :
            out_file.write(riga) 
    out_file.close()
    return out_file

为什么要多次打开/关闭文件

def clearfile(filename):
    out_file = open('%s_%s%s' %(os.path.splitext(filename)[0], 'ok', os.path.splitext(os.path.basename(filename))[1]) , 'a')
    for row in (open (filename, 'r')).readlines():
        if row[0][0]!= '\n' and riga[0][0]!='(' :
            out_file.write(riga) 
    out_file.close()
    return out_file

试着这样做:

import os
def clear_and_move(infname, outfname):
    with open(infname, 'r') as inf, open(outfname, 'w+') as outf:
        # write new file as a copy of the first
        # skip lines that contain only whitespace or start with '('
        outf.writelines(line for line in inf 
                        if line.strip() and not line.startswith('('))
    # delete the original file
    os.remove(infname)

试着这样做:

import os
def clear_and_move(infname, outfname):
    with open(infname, 'r') as inf, open(outfname, 'w+') as outf:
        # write new file as a copy of the first
        # skip lines that contain only whitespace or start with '('
        outf.writelines(line for line in inf 
                        if line.strip() and not line.startswith('('))
    # delete the original file
    os.remove(infname)

”是去擦亮的,用“
!=
”代替!如果你喜欢你的解决方案,你可以把它作为答案发布。
”是去擦亮的,用“
!=
”代替!如果你喜欢你的解决方案,你可以把它作为答案发布。用语句处理文件。@AshwiniChaudhary你是对的,我真的应该学习这句话。谢谢你的帮助link.use语句用于处理文件。@AshwiniChaudhary你说得对,我真的应该学习这个语句。谢谢你的链接。