如何在python中将变量值传递给字符串

如何在python中将变量值传递给字符串,python,Python,我需要从json文件中读取一个字符串,并为其赋值,使其成为一个可配置的文件名 我从json读取的字符串: data_file_format = "sourcesystem_batchid_extractname_loadtype" 我有一个变量,它保存代码中的值,如 sourcesystem ="xyz" batchid = "101" extractname = "abc" loadtype = "Delta" 所以我的数据文件格式应该是 data_file_format =

我需要从json文件中读取一个字符串,并为其赋值,使其成为一个可配置的文件名

我从json读取的字符串:

data_file_format =  "sourcesystem_batchid_extractname_loadtype"
我有一个变量,它保存代码中的值,如

sourcesystem ="xyz"
  batchid = "101"
  extractname = "abc"
  loadtype = "Delta"
所以我的数据文件格式应该是

data_file_format = "xyz_101_abc_Delta"

您可以使用+


这将为您提供xyz_101_abc_Delta

,您可以使用+

sourcesystem ="xyz"
batchid = "101"
extractname = "abc"
loadtype = "Delta"
data_file_format="_".join([sourcesystem,batchid,extractname,loadtype])
#or
#data_file_format=sourcesystem+'_'+batchid +'_'+extractname+'_'+loadtype
print(data_file_format)

这将为您提供xyz_101_abc_Delta

有多种方法可以做到这一点:

sourcesystem ="xyz"
batchid = "101"
extractname = "abc"
loadtype = "Delta"
data_file_format="_".join([sourcesystem,batchid,extractname,loadtype])
#or
#data_file_format=sourcesystem+'_'+batchid +'_'+extractname+'_'+loadtype
print(data_file_format)
fstring

data_file_format = f'{sourcesystem}_{batchid}_{extractname}_{loadtype}'
或者使用.format

data_file_format = '{}_{}_{}_{}'.format(sourcetype,batchid,extractname,loadtype)

有多种方法可以做到这一点:

fstring

data_file_format = f'{sourcesystem}_{batchid}_{extractname}_{loadtype}'
或者使用.format

data_file_format = '{}_{}_{}_{}'.format(sourcetype,batchid,extractname,loadtype)

或者你可以选择基本的,然后像

data_file_format = datasourcesystem + "_" + batchid + "_" + extractname + "_" + loadtype

前面的答案也不遥远。它只是忘记了下划线

或者你可以选择基本的,然后继续这样做

data_file_format = datasourcesystem + "_" + batchid + "_" + extractname + "_" + loadtype

前面的答案也不遥远。它只是忘记了下划线

,因此,您需要根据输入数据文件格式动态生成文件名。你能将数据存储在dict中而不是单独的变量中吗

data_file_format =  "sourcesystem_batchid_extractname_loadtype"

data = {
  "sourcesystem": "xyz",
  "batchid": "101",
  "extractname": "abc",
  "loadtype": "Delta"
}

filename = '_'.join([data[key] for key in data_file_format.split('_')])

print(filename)
xyz_101_abc_三角洲

sourcesystem ="xyz"
batchid = "101"
extractname = "abc"
loadtype = "Delta"
data_file_format="_".join([sourcesystem,batchid,extractname,loadtype])
#or
#data_file_format=sourcesystem+'_'+batchid +'_'+extractname+'_'+loadtype
print(data_file_format)

因此,您需要根据输入数据文件格式动态生成文件名。你能将数据存储在dict中而不是单独的变量中吗

data_file_format =  "sourcesystem_batchid_extractname_loadtype"

data = {
  "sourcesystem": "xyz",
  "batchid": "101",
  "extractname": "abc",
  "loadtype": "Delta"
}

filename = '_'.join([data[key] for key in data_file_format.split('_')])

print(filename)
xyz_101_abc_三角洲

sourcesystem ="xyz"
batchid = "101"
extractname = "abc"
loadtype = "Delta"
data_file_format="_".join([sourcesystem,batchid,extractname,loadtype])
#or
#data_file_format=sourcesystem+'_'+batchid +'_'+extractname+'_'+loadtype
print(data_file_format)
.join将是此场景中的最佳答案。答案由bitto提供。但对于未来,一些好方法是:

>>> a = 1
>>> b = 2
>>> c = "three"
>>> "%d and %d then %s" % (a, b, c)
1 and 2 then three
>>> "{} and {} then {}".format(a, b, c)
1 and 2 then three
>>> f"{a} and {b} then {c}"
1 and 2 then three
.join将是此场景中的最佳答案。答案由bitto提供。但对于未来,一些好方法是:

>>> a = 1
>>> b = 2
>>> c = "three"
>>> "%d and %d then %s" % (a, b, c)
1 and 2 then three
>>> "{} and {} then {}".format(a, b, c)
1 and 2 then three
>>> f"{a} and {b} then {c}"
1 and 2 then three

看这个,特别是这个。看这个,特别是这个。我想动态生成这个。。由于我的文件格式因文件而异,我希望动态生成此文件。。因为我的文件格式因文件而异