I have the following string:
monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date
and the following Python script:
from datetime import datetime, date, time
today = datetime.now()
print today
print today
with open('calendar_clean.txt', 'w') as f1:
with open('calendar.txt', 'r') as f:
newline = ""
end_date_index = -1
for line in f:
items = line.split(",")
if end_date_index == -1:
for item in items:
print item
print "finding end_date index"
end_date_index = items.index('end_date')
print end_date_index
When I execute script, the following error message is displayed on the console:
ValueError: 'end_date' is not in list
If I look for the index of start_date, however, it executes normally, returning correct result. This is using Python 2.7 on Windows 7 x64. Any ideas out there?
You have a line terminator on the end of your line. So it’s looking for something like “end_date\n” or “end_date\r”, not just “end_date”. Use
line.rstrip().split(",")instead of only callingsplit.