我正在尝试阅读csv文件并在其中进行搜索。
我想找到一种将CSV文件中的标题用作变量的方法。这可能吗?
with open("Server.csv", newline='', encoding='utf-8', errors='ignore') as f :
reader = csv.reader(f)
Header = next(reader)
# This part below is to get the Header on the file.
Header_array = []
for n in range(len(Header)):
header_list = Header[n].replace(" ", "_")
Header_array.append(header_list)
print(Header_array) # this give me a list
我想要提取的变量是
Application_Name, Environment, server_hostname, OverallStatus, Migration_Date
如何根据以下代码使用此标头作为动态变量?假设我明天在csv文件中添加“Time”,我不需要关心在下面添加Time = row [x:x] [x]。
for row in reader :
Application_Name = row[0:1][0]
Environment = row[1:2][0]
server_hostname = row[2:3][0]
OverallStatus = row[3:4][0]
Migration_Date = row[4:5][0]
return Application_Name, server_hostname