Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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_Python 2.7 - Fatal编程技术网

Python 多个文件(从起始行和结束行删除相同的字符串)

Python 多个文件(从起始行和结束行删除相同的字符串),python,python-2.7,Python,Python 2.7,我有多个文件。每个文件的格式如下所示: <float> <int> <stringSAME> <float> <int> <stringSAME> <float> <int> <string> ...... <float> <int> <stringSAME> ...... ...... <float> <int> <str

我有多个文件。每个文件的格式如下所示:

<float> <int> <stringSAME>
<float> <int> <stringSAME>
<float> <int> <string>
......
<float> <int> <stringSAME>
......
......
<float> <int> <string>
<float> <int> <stringSAME>
<float> <int> <stringSAME>

......
......
......
此处,第1行和第2行中的字符串与最后几行中的字符串相同。它被表示为stringSAME。现在我想从文件的开头和结尾删除这个字符串。但在这两者之间保持不变。本程序适用于具有相同格式的多个文件。
请提出一些解决办法。我正在使用python作为我的编程语言

有几种方法可以做到这一点,具体取决于您以后想如何使用它

如果您只是想将这些行用作数据,我们可以:

with open("path/to/file") as f:
    data = (line for line in f if not line.endswith("<stringSAME>"))
您还可以将其写入文件的新副本:

with open("path/to/sanitized_file","w") as f:
    for line in data:
        f.write(line+"\n")
如果您试图在多个文件上执行此操作,请首先构建一个列表

import os

list_of_files = ["file1.txt","file2.txt","file3.txt"]
for file in list_of_files:
    in_file = os.path.join("path","to",file)
    out_file = os.path.join("path","to","post proc",file)

# if you have to do it to a whole directory worth of files, try this instead
## import glob
## list_of_files = glob.glob("path/to/dir/*")
## for file in list_of_files:
##     in_file = file
##     head,tail = os.path.split(file)
##     out_file = os.path.join(head,"post proc",tail)
# which simplifies a lot of the following, since none of the FileNotFoundErrors
# should ever trigger, other than the one in case the post proc directory
# doesn't exist

    try:
        with open(in_file, 'r') as f:
            data = (line for line in f if not line.endswith("<stringSAME>"))
    except IOError as e:
        # log and handle error if you can't open file
    except FileNotFoundError as e:
        # log and handle error if the file isn't there
    try:
        with open(out_file,'w') as f:
            for line in data:
                f.write(line+"\n")
    except IOError as e:
        # log and handle error if you can't write to file
    except FileNotFoundError as e:
        # log and handle error if the directory doesn't exist
导入操作系统
文件列表=[“file1.txt”、“file2.txt”、“file3.txt”]
对于\u文件列表中的文件:
in_file=os.path.join(“path”,“to”,file)
out\u file=os.path.join(“path”、“to”、“post-proc”、file)
#如果必须对整个目录的文件执行此操作,请尝试此操作
##导入glob
##文件列表=glob.glob(“path/to/dir/*”)
##对于\u文件列表中的文件:
##in_file=file
##head,tail=os.path.split(文件)
##out\u file=os.path.join(head,“post-proc”,tail)
#这简化了以下许多操作,因为没有FileNotFound错误
#应该触发,而不是在post proc目录中触发
#不存在
尝试:
将打开的(在_文件中,'r')作为f:
数据=(如果不是line.endswith(“”),则f中的行对应一行)
除IOE错误外:
#如果无法打开文件,请记录并处理错误
除FileNotFoundError为e外:
#如果文件不存在,则记录并处理错误
尝试:
打开(out_文件,'w')作为f:
对于行输入数据:
f、 写入(第+行“\n”)
除IOE错误外:
#如果无法写入文件,请记录并处理错误
除FileNotFoundError为e外:
#如果目录不存在,则记录并处理错误

是要删除整行还是仅删除部分?是的,我要删除整行!因此,对于文件中以
结尾的任何行,我们要删除该行吗?这很简单,但我想确保我们的观点一致。是的!这些都存在于同一文件中上述代码不工作ValueError:关闭时的I/O操作file@U-检查你的缩进
f
仅在
with
块中打开,因此如果缩进减少了1,则在缩进关闭后,您将尝试读取(或写入)
f
import os

list_of_files = ["file1.txt","file2.txt","file3.txt"]
for file in list_of_files:
    in_file = os.path.join("path","to",file)
    out_file = os.path.join("path","to","post proc",file)

# if you have to do it to a whole directory worth of files, try this instead
## import glob
## list_of_files = glob.glob("path/to/dir/*")
## for file in list_of_files:
##     in_file = file
##     head,tail = os.path.split(file)
##     out_file = os.path.join(head,"post proc",tail)
# which simplifies a lot of the following, since none of the FileNotFoundErrors
# should ever trigger, other than the one in case the post proc directory
# doesn't exist

    try:
        with open(in_file, 'r') as f:
            data = (line for line in f if not line.endswith("<stringSAME>"))
    except IOError as e:
        # log and handle error if you can't open file
    except FileNotFoundError as e:
        # log and handle error if the file isn't there
    try:
        with open(out_file,'w') as f:
            for line in data:
                f.write(line+"\n")
    except IOError as e:
        # log and handle error if you can't write to file
    except FileNotFoundError as e:
        # log and handle error if the directory doesn't exist