从csv字段中删除空格但忽略datetime objec

时间:2017-11-24 16:50:52

标签: python csv datetime

我有一个csv,它是DB2查询的结果。 出于某种原因,csv就像这样创建

"filed1     ", "field2,     ","2017-11-24"

我可以用以下方法删除字段内的空白区域:

for result in results:
    result = [x.strip(' ') for x in result]
    csvwriter.writerow(result)

但日期字段为<type 'datetime.date'>,因此我收到了错误

AttributeError: 'datetime.date' object has no attribute 'strip'

如何将strip函数仅应用于字符串对象?或者我可以在str对象中转换datetime.date对象吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

只需检查之前的类型:

if isinstance(x,str):
    ...