csv 产生的数据都是字符串类型的,它不会做任何其他类型的转换。如果需要做这样的类型转换,必须自己手动去实现

import csv,re
from collections import namedtuple
col_types=[str,float,str,str,str,int]
with open(r'C:\Temp\ff.csv') as f:
    f_csv=csv.reader(f)
    headers=next(f_csv)
    print(headers)
    Row=namedtuple('Row',headers)
    for r in f_csv:
        row=Row(*r)
        print(row)
        print(type(row.IssueType))
        row=tuple(convert(value) for convert,value in zip(col_types,row))
        print(row)

 python 读csv文件时,在csv类型上执行类型转换

 

相关文章:

  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-02
  • 2021-11-13
  • 2022-02-04
  • 2022-02-23
猜你喜欢
  • 2022-12-23
  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
  • 2021-07-05
  • 2021-07-21
  • 2022-12-23
相关资源
相似解决方案