Reputation:
I'm trying to parse a file that contains, a bunch of entries which, among other fields, contains a date in its last column.
Walmart,Retail,482,-0.7,2200000,Arkansas,31-10-1969
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]
data.append(rec)
readf.close()
print('\nDone.\n\n')
return data
But when I try to convert the last field of the line (the date) to an actual datetime.datetime
instance, I get the following error:
data_string[found.end():])
ValueError: unconverted data remains:
the full error stack is
Traceback (most recent call last):
File "C:\Users\Keitha Pokiha\Desktop\New folder\Program 2.py", line 42, in <module>
main()
File "C:\Users\Keitha Pokiha\Desktop\New folder\Program 2.py", line 39, in main
data = readdata('fname.txt')
File "C:\Users\Keitha Pokiha\Desktop\New folder\Program 2.py", line 12, in readdata
date = datetime.strptime(date1,'%d-%m-%Y')
File "C:\Users\Keitha Pokiha\AppData\Local\Programs\Python\Python35-32\lib\_strptime.py", line 510, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "C:\Users\Keitha Pokiha\AppData\Local\Programs\Python\Python35-32\lib\_strptime.py", line 346, in _strptime
data_string[found.end():])
ValueError: unconverted data remains:
Upvotes: 1
Views: 416
Reputation: 18438
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
:
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')
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(',')
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.
Upvotes: 2