从其他fi迭代导入文件名

2024-04-23 15:13:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个文件中有大量的条目。我叫它文件A

文件A:

('aaa.dat', 'aaa.dat', 'aaa.dat')

('aaa.dat', 'aaa.dat', 'bbb.dat')

('aaa.dat', 'aaa.dat', 'ccc.dat')

我想在一个程序中逐行使用这些条目,该程序将迭代地从文件a中选取条目,并以这种方式连接文件:

filenames = ['aaa.dat', 'aaa.dat', 'ccc.dat'] ###entry number 3
with open('out.dat', 'w') as outfile:   ###the name has to be aaa-aaa-ccc.dat
    for fname in filenames:
        with open(fname) as infile:
            outfile.write(infile.read().strip()) 

我所需要做的就是迭代地替换文件名,并在“aaa”中创建一个输出-aaa.dat公司“格式。我会很感激任何帮助--感觉有点失落!你知道吗

多谢!!!你知道吗


Tags: 文件程序aswith方式条目openfname
3条回答

像这样:

with open(fname) as infile, open('out.dat', 'w') as outfile:
    for line in infile:
        line = line.strip()
        if line:  # not empty
            filenames = eval(line.strip())  # read tuple
            filenames = [f[:-4] for f in filenames]  # remove extension
            filename = '-'.join(filenames) + '.dat'  # make filename
            outfile.write(filename + '\n')  # write

您可以通过以下方式检索和修改文件名:

import re
pattern = re.compile('\W')

with open('fnames.txt', 'r') as infile:
    for line in infile:
        line = (re.sub(pattern, ' ', line)).split()
        # Old filenames - to concatenate contents
        content = [x + '.dat' for x in line[::2]];
        # New filename
        new_name = ('-').join(line[::2]) + '.dat'
        # Write the concatenated content to the new
        # file (first read the content all at once) 
        with open(new_name, 'w') as outfile:
            for con in content:
                with open(con, 'r') as old:
                    new_content = old.read()
                    outfile.write(new_content)

这个程序逐行读取您的输入文件,这里的文件名为fnames.txt,其结构与您的文章完全相同。对于每一行,它使用一个预编译的regex(预编译regex在这里是合适的,应该会使事情变得更快)。这假设您的文件名只是字母数字字符,因为regex用空格替换所有非字母数字字符。你知道吗

它只检索'aaa'dat条目作为每行的字符串列表,并通过连接从0开始的第二个条目并向其添加.dat扩展名来形成一个新名称。它在post中使用-连接。你知道吗

然后通过从line中选择第二个条目,检索各个文件名,从中将内容提取到列表content。你知道吗

最后,它读取content中的每个文件,并将它们写入公共文件new_name。如果这些文件很大的话,它会在一个位置上读取所有这些文件,这可能是一个问题,一般来说,可能有更有效的方法来完成这一切。另外,如果您计划在编写之前对旧文件中的内容执行更多操作,请考虑将旧文件特定的操作移动到单独的函数中,以提高可读性和任何潜在的调试。你知道吗

如果您的问题只是计算新的文件名,那么使用^{}如何?你知道吗

'-'.join([
   f[0] for f in [os.path.splitext(path) for path in filenames]
]) + '.dat'

如果你这样看的话,可能会更好地理解:

import os

clean_fnames = []
filenames = ['aaa.dat', 'aaa.dat', 'ccc.dat']
for fname in filenames:
    name, extension = os.path.splitext(fname)
    clean_fnames.append(name)

name_without_ext = '-'.join(clean_fnames)
name_with_ext = name_without_ext + '.dat'
print(name_with_ext)

但是:如果您的问题是无法通过逐行读取文件来获取列表中的filenames,则必须记住,当您读取文件时,将获取文本(字符串)而不是Python结构。您需要从如下文本重建list"('aaa.dat', 'aaa.dat', 'aaa.dat')\n"。你知道吗

您可以查看^{}或尝试自己重建它。下面的代码输出许多消息来显示发生了什么:

import pprint

collected_fnames = []
with open('./fileA.txt') as f:
    for line in f:
        print("Read this (literal) line: %s" % repr(line))
        line_without_whitespaces_on_the_sides = line.strip()
        if not line_without_whitespaces_on_the_sides:
            print("line is empty... skipping")
            continue
        else:
            line_without_parenthesis = (
                line_without_whitespaces_on_the_sides
                .lstrip('(')
                .rstrip(')')
            )
            print("Cleaned parenthesis: %s" % line_without_parenthesis)
            chunks = line_without_parenthesis.split(', ')
            print("Collected %s chunks in a %s: %s" % (len(chunks), type(chunks), chunks))
            chunks_without_quotations = [chunk.replace("'", "") for chunk in chunks]
            print("Now we don't have quotations: %s" % chunks_without_quotations)
            collected_fnames.append(chunks_without_quotations)

print("collected %s lines with filenames:\n%s" %
      (len(collected_fnames), pprint.pformat(collected_fnames)))

相关问题 更多 >