TransWikia.com

ValueError: Unconverted data remains .000

Stack Overflow Asked by vbdashes on November 29, 2021

I’m super new to Python and just got thrown for a loop when my source csv file changed its format. The date field now has this: 2020-07-22T00:00:00.000 when what I want is this: 2020-07-22.

I need to read the source csv and append it to a database table. I looked at some other questions and found this sample code which I think will do what I want (create a copy of the csv with the newly formatted date which I can use with the rest of my code).

import csv
from datetime import datetime

with open(csvCasesVH, 'r') as source:
    with open(csvCasesVH2, 'w') as result:
        writer = csv.writer(result, lineterminator='n')
        reader = csv.reader(source)
        source.readline()
        for row in reader:
            ts = datetime.strptime(row[0], '%Y-%m-%dT%H:%M:%S').strftime("%Y-%m-%d")
            print(ts)
            row[0]=ts
            if ts != "":
                writer.writerow(row)
source.close()
result.close()

I’m trying to figure out the datetime format – almost got there but now that this .000 hanging out there I don’t know what to do with:

ValueError: unconverted data remains: .000

I would appreciate any advice. Thanks.

2 Answers

The strptime function tries to get a datetime object out of a string, to do that it needs a datetime string and it needs a format matching the string. For example lets say I have the string '25/07/2020 21:13', to extract a date I need to give it the format '%d/%m/%Y %H:%M', if I gave it the format '%d/%m/%Y %H:' it would give the error ValueError: unconverted data remains: 13 because it doesn't know what the 13 stands for (minutes?, seconds?, am/pm?). To solve this problem you can look at how the string looks like by printing it and modifying the format.

A useful site to use when working with datetime formats is https://strftime.org/.

In your case the remaining .000 probably means microseconds so you need to add it to the format: '%Y-%m-%dT%H:%M:%S.%f' (%f stands for microsecconds)

Answered by MrPostduif on November 29, 2021

Try updating the line:

ts = datetime.strptime(row[0], '%Y-%m-%dT%H:%M:%S').strftime("%Y-%m-%d")

To:

ts = datetime.strptime(row[0], '%Y-%m-%dT%H:%M:%S.%f').strftime("%Y-%m-%d")

Note the .%f which is for microseconds

Answered by Spencer Bard on November 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP