Bodine taukiri Bodine taukiri - 1 year ago 243
Python Question

Python: Date/time format

I'm trying to parse a file that contains, a bunch of entries which, among other fields, contains a date in its last column.


I've tried doing this:

from datetime import datetime

def readdata (fname):

print ('*'*5,'Reading Records From File',fname,'*'*5)

data = []

readf = open(fname,'r')
for line in readf:
name1, name2, No_1, No_2, No_3, name3, date1 = line.split(',')
date = datetime.strptime(date1,'%d-%m-%Y')
Number1 = float(No_1)
Number2 = float(No_2)
Number3 = int(No_3)

rec = [name1,name2,Number1,Number2,Number3,name3,date]
return data

But when I try to convert the last field of the line (the date) to an actual
instance, I get the following error:

ValueError: unconverted data remains:

the full error stack is

Traceback (most recent call last):
File "C:\Users\Keitha Pokiha\Desktop\New folder\Program", line 42, in <module>
File "C:\Users\Keitha Pokiha\Desktop\New folder\Program", line 39, in main
data = readdata('fname.txt')
File "C:\Users\Keitha Pokiha\Desktop\New folder\Program", line 12, in readdata
date = datetime.strptime(date1,'%d-%m-%Y')
File "C:\Users\Keitha Pokiha\AppData\Local\Programs\Python\Python35-32\lib\", line 510, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "C:\Users\Keitha Pokiha\AppData\Local\Programs\Python\Python35-32\lib\", line 346, in _strptime
ValueError: unconverted data remains:

Answer Source

The problem that you seem to be having is that when you do for line in readf:, line ends with the carriage return (special character \n, which signals a new line) so instead of trying to convert 31-10-1969 to datetime, Python is trying to convert 31-10-1969\n, using the format %d-%m-%Y Therefore, when it finishes parsing the year (%Y) it finds an unexpected \n and that's why you're seeing that error: because it doesn't know what to do with it.

You have several options to fix this. Below you'll find two that "fix" the read line, and a third that "fixes" the format expected by datetime:

  1. You can remove that \n it using rstrip after you've read the line:

    name1, name2, No_1, No_2, No_3, name3, date1 = line.rstrip().split(',')
    date = datetime.strptime(date1, '%d-%m-%Y')
  2. Or you could use the method explained here and remove the last character in the line, like this:

    name1, name2, No_1, No_2, No_3, name3, date1 = line[:-1].split(',')
  3. Or you could tell the datetime module to expect a newline as well in the string:

    name1, name2, No_1, No_2, No_3, name3, date1 = line.split(',')
    date = datetime.strptime(date1, '%d-%m-%Y\n')

I'd use 1., because if your line doesn't end with a newline character, everything will still work.

PS (as a side note): If you're reading a comma-separated-value file, I'd strongly suggest you make use of the csv.reader module.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download