Python - 将列表中的项目作为列传递给 pandas read_csv 方法

Python - passing items from a list as columns for pandas read_csv method

我想将几个从网络表单输入的值转换为单个 html 文件。 这是我现在的代码

formData = cgi.FieldStorage()
my_file = formData.getvalue("my_filename")
my_col = formData.getlist("column")

print(my_col) #list

for item in my_col:
    csvdata = pandas.read_csv(my_file, usecols=[item])

#cd = csvdata.to_html()
#...

显然这只会转换为 html 列表中的最后一项。如何将 my_col 列表中的每个项目转换为单个 html 文件?

您只获得了最后一个元素,因为您正在循环 my_cols。您不需要这样做,而是按如下方式将整个 list my_cols 发送到 usecols,您将获得所需的结果。


formData = cgi.FieldStorage()
my_file = formData.getvalue("my_filename")
my_col = formData.getlist("column")

print(my_col) #list

csvdata = pandas.read_csv(my_file, usecols=my_col)