为什么我的字符串被转换成一个元组,当我把它附加到我的列表?



我正在从csv文件中读取行,并使用它来启动VM类的实例,然后将其附加到一个名为vm_list的列表中。我添加了两个不同的ip地址,&;ip_address&;和"ilo_ip,分别是第[1]行和第[2]行。我注意到ilo_ip是作为元组添加的,但是如果我在添加之前检查类型,它是一个字符串。我甚至尝试在我的append语句中将其类型转换为字符串,以确保它作为字符串通过,但它没有。知道这是为什么吗?

Im using
Python 3.9.6

linux_inventory.csv:

testname 10.10.10.10 11.11.11.11 fake_location fake_classification fake_server_type fake_end_of_life fake_os_version fake_domain fake_serial_number fake_function

reader.py

import csv
#virtual_machine
class vm:
def __init__(self, name, ip_address, ilo_ip, location, classification, server_type, end_of_life, os_version, domain, serial_number, function):
self.name = name
self.ip_address = ip_address
self.ilo_ip = ilo_ip, 
self.location = location,
self.classification = classification, 
self.server_type = server_type,
self.end_of_life = end_of_life
self.os_version = os_version
self.domain = domain
self.serial_number = serial_number
self.function = function

vm_list = []
with open('linux_inventory.csv', newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:

vm_list.append(vm(row[0],row[1],str(row[2]),row[3],row[4],row[5],row[6],row[7],row[8],row[9],row[10]))
print(row) # output: ['testname', '10.10.10.10', '11.11.11.11', 'fake_location', 'fake_classification', 'fake_server_type', 'fake_end_of_life', 'fake_os_version', 'fake_domain', 'fake_serial_number', 'fake_function']
print(row[2]) #output 11.11.11.11
print(type(row[2])) #output: <class 'str'>


for vm in vm_list:
print(vm.ip_address) #output: 10.10.10.10
print(type(vm.ip_address)) # output: <class 'str'>
print(vm.ilo_ip) #output: 11.11.11.11
print(type(vm.ilo_ip)) #output: <class 'tuple'>

这就是问题所在

self.ilo_ip = ilo_ip, # this is equivalent to self.ilo_ip = (ilo_ip,)
self.location = location,
self.classification = classification, 
self.server_type = server_type,

在python中,元组不需要用括号定义。一个逗号就足够了。去掉每行末尾的逗号,应该是固定的。

self.ilo_ip = ilo_ip
self.location = location
self.classification = classification
self.server_type = server_type