使用f字符串从for循环的输出进行变量赋值

时间:2018-12-20 16:36:11

标签: python-3.x for-loop f-string

我正在尝试编写一个目前应该的python程序

  1. 更改为基本目录中的所有子目录(当我运行python脚本时使用自定义参数指定)
  2. 在每个子目录中,程序应查找名为SampleSheet.csv的文件,并使用以下模式进行保存:ss_tab_string_pattern,其中pattern是{{1}的字符串}输出,如下面的代码所示。

为了阐明我的问题,可以在下面找到带有相应注释的代码:(子目录for loop是通过编程方式获取的,但是在这里,为了简单起见,我将它们放入列表中)

subdirs

运行此代码时,出现以下错误消息:

subdirs = ['180301_A00154_0026_BH7T5GDMXX', '181127_A00154_0121_AH52WHDRXX', '181205_A00154_0129_BH55W7DRXX', '181210_A00154_0131_BH573KDRXX']

import os
for i in list(range(len(subdirs))):
    os.chdir(args.bd + '\\' + subdirs[i]) # change to a subdirectory. `args.bd` is the full path to my basedirectory and each element in the list `subdirs` is a subdirectory of `args.bd`
    uniq_identifier = subdirs[i].split('_')[-2] # here I am fetching unique identifiers from the elements in `subdirs` (i.e. 0026, 0121, 0129 and 0131)
    with open('SampleSheet.csv') as f:
        f'ss_tab_string_{uniq_identifier}' = f.read() # here is where I am having problems. Apparently I cannot use f string for variable assignment.

考虑到此示例,我的预期输出将是获取名为can't assign to literal ss_tab_string_0026ss_tab_string_0121ss_tab_string_0129的四个对象(str类型),而每个对象的内容是否会像在ss_tab_string_0131列表中那样在每个子目录中找到SampleSheet.csv

0 个答案:

没有答案
相关问题