在创建CSV时,我正试图从python中的配置文件中读取一些头值。
工作Python文件:
headers = ['Column1','Column2','Column3']
...
writer.writerow(header)
...
使用配置:text.conf
[CSV]
headers = 'Column1','Column2','Column3'
Python文件
config = configparser.ConfigParser()
config.read(text.conf)
header = config['CSV'].get('headers')
但我的csv文件看起来像这样,
',C,o,l,u,m,n,1,',",",',C,o,l,u,m,n,2,',",",',C,o,l,u,m,n,3,'
预期:
Column1,Column2,Column3
您得到的是字符串对象,而不是列表。您可以将字符串处理为列表
例如:
config = configparser.ConfigParser()
config.read(text.conf)
header = [i.strip("'") for i in config['CSV'].get('headers').split(",")]
或添加[]
->headers = ['Column1','Column2','Column3']
到配置文件,并使用ast
模块将其转换为列表
例如:
headers = ['Column1','Column2','Column3']
print(ast.literal_eval(config['CSV']['headers']))