0

我正在尝试根据包含试用名称的用户输入列表将某些文件信息记录到字典中。

def health(input):
    file_location = "./data/health_data/"
    dictionary= {}

    return(dictionary)

我在 health_data 文件夹中有一组文件,例如 010.csv 011.csv 012.csv 等。

用户可以输入药物试验列表,如下所示:

input = ["010", "011"] 

所以我想要做的是运行一个循环,以便将每个列表项连接到 file_location 以便它看起来像:


for i in input:

    file_location ="./data/health_data/i.csv"


我正在努力将输入连接到文件位置字符串中。我已经尝试 .join()并尝试添加它们,但我得到的错误是只有 str 可以连接到 str,而不是“列表”

如何将输入中给出的列表转换为字符串,然后将其连接到 file_location 变量?

4

1 回答 1

1

假设您使用的是 Python 3.6+,您可以使用f-string

for i in input:
    file_location = f"./data/health_data/{i}.csv"
    do_stuff()
于 2020-10-17T22:41:38.293 回答