简体   繁体   English

文件输入作为行与文件输入作为字符串

[英]FileInput as line versus fileinput as string

I have a list of files that I want to iterate over with RegEx replacements, some on individual lines, some that require multiline matches.我有一个文件列表,我想用 RegEx 替换来迭代,一些在单独的行上,一些需要多行匹配。

I am able to iterate over lines in a list of files and write to disk using this method.我能够遍历文件列表中的行并使用此方法写入磁盘。

import fileinput, re

ListFiles = ['in/spam.txt', 'in/eggs.txt', 'in/spam2.txt', 'in/eggs2.txt', 
'in/spam3.txt', 'in/eggs3.txt', 'in/spam4.txt', 'in/eggs4.txt',
'in/spam5.txt', 'in/eggs5.txt']

with fileinput.input(files=(ListFiles), inplace=True, backup='.bak') as f:
    for line in f:
        line = re.sub(r'this','that', line)
        print(line, end='')

Now I want to gather the output lines in f as a string, for which I can run multiline RegEx routines.现在我想将f的输出行收集为一个字符串,为此我可以运行多行 RegEx 例程。

I tried a with(open) , which I have been able to use to use with ReGex a single file, but it does not take a list as an argument, only a file name.我尝试了with(open) ,我已经能够使用它与 ReGex 一起使用单个文件,但它不接受列表作为参数,只有文件名。

with open("spam.txt", "w") as f: # sample other use, list not allowed here.
    data = f.read()
    data = re.sub(r'sample', r'sample2', data)
    print(data, file=f)

And I tried to gather f as a string into new variable data, as follows:我尝试将f作为字符串收集到新的变量数据中,如下所示:

data = f(str)
data = re.sub(r'\\sc\{(.*?)\}', r'<hi rend="small_caps">\1</hi>', data) ## Ignore that this not multiline Regex for sample purposes only.
print(data)

But that produces error, that FileInput is not callable.但这会产生错误,即 FileInput 不可调用。

Is there a way that I can iterate and apply RegEx to files as line and as the same files as string in same with statement?有没有一种方法可以迭代并将 RegEx 作为行和与字符串相同的文件应用于与语句相同的文件?

If it is ok to read individual files into memory as a whole then to perform multiline replacements in a list of files, you could process one file at a time:如果可以将单个文件作为一个整体读入内存,然后在文件列表中执行多行替换,则可以一次处理一个文件:

for filename in ListFiles:
    with open(filename) as file: 
        text = file.read() # read file into memory
    text = text.replace('sample\n1', 'sample2') # make replacements
    with open(filename, 'w') as file: 
        file.write(text) # rewrite the file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM