Why isn't Django rendering my template? - python

I'm working on a Django application and I'm just trying to push data up to the front-end to display.
In my views.py here's what I have:
def index(request):
...
context = RequestContext(request)
rooms = dict(db.studybug.find_one())
timeRange = [room.encode('utf-8') for room in rooms['timeRange']]
return render_to_response('studybug/index.html', timeRange, context)
Here, timeRange is a list that contains the following:
timeRange = ['Room 203A 10:00 AM \xc2\xa0', 'Room 203A 10:30 AM \xc2\xa0', 'Room 203A 11:00 AM \xc2\xa0', 'Room 203A 11:30 AM \xc2\xa0', 'Room 203A 12:00 PM \xc2\xa0', 'Room 203A 12:30 PM \xc2\xa0', 'Room 203A 3:00 PM \xc2\xa0', 'Room 203A 3:30 PM \xc2\xa0', 'Room 203A 4:00 PM \xc2\xa0', 'Room 203A 4:30 PM \xc2\xa0', 'Room 203A 5:00 PM \xc2\xa0', 'Room 203A 5:30 PM \xc2\xa0', 'Room 203A 6:00 PM \xc2\xa0', 'Room 203A 6:30 PM \xc2\xa0', 'Room 203A 7:00 PM \xc2\xa0', 'Room 203A 7:30 PM \xc2\xa0', 'Room 203A 8:00 PM \xc2\xa0', 'Room 203A 8:30 PM \xc2\xa0', 'Room 203A 9:00 PM \xc2\xa0', 'Room 203A 9:30 PM \xc2\xa0', 'Room 203A 10:00 PM \xc2\xa0', 'Room 203A 10:30 PM \xc2\xa0', 'Room 203A 11:00 PM \xc2\xa0', 'Room 203A 11:30 PM \xc2\xa0']
And then in my template (index.html), I have the following loop:
<div class="row">
...
<ul>
{% for item in timeRange %}
<li>{{ item }}</li>
{% endfor %}
</ul>
</div>
However, despite the list being generated in the backend, nothing is being displayed on the webpage. I know the list exists, but Django's rendering engine won't display it.
Am I missing something obvious here?
Thanks,
G

render_to_response second parameter, should be a dict containing your data, you are passing a list.
your render_to_response should looks like this:
return render_to_response('studybug/index.html', {'timeRange':timeRange}, context)

Related

all possible time delta combinations in python

I have a time period from 9:00 till 22:00 and I need to list all possible durations with a step of 30 minutes within this period. E.g.
9:00 - 9:30
9:00 - 10:00
9:00 - 10:30
...
21:00 - 22:00
21:30 - 22:00
I've googled and found itertools.combinations() for numbers but nothing comparable for dates

Format response content with python requests module

I have a web page which I can access from my server. The contents of the web page are as below.
xys.server.com - /xys/reports/
[To Parent Directory]
3/4/2021 6:09 AM <dir> All_Master
3/4/2021 6:09 AM <dir> Hartland
3/4/2021 6:09 AM <dir> Hauppauge
3/4/2021 6:09 AM <dir> Hazelwood
2/15/2019 7:41 AM 58224 NetBackup Retention and Full Backup Occupancy.xlsx
1/1/2022 11:00 AM 23959 OpsCenter_All_Master_Server_Backup_Report_01_01_2022_10_00_45_259_AM_49.zip
2/1/2022 11:00 AM 18989 OpsCenter_All_Master_Server_Backup_Report_01_02_2022_10_00_04_813_AM_4.zip
3/1/2022 11:00 AM 18969 OpsCenter_All_Master_Server_Backup_Report_01_03_2022_10_00_24_664_AM_17.zip
4/1/2021 10:00 AM 21709 OpsCenter_All_Master_Server_Backup_Report_01_04_2021_10_00_02_266_AM_31.zip
5/1/2021 10:00 AM 27491 OpsCenter_All_Master_Server_Backup_Report_01_05_2021_10_00_27_655_AM_11.zip
6/1/2021 10:00 AM 21260 OpsCenter_All_Master_Server_Backup_Report_01_06_2021_10_00_54_053_AM_19.zip
7/1/2021 10:00 AM 19898 OpsCenter_All_Master_Server_Backup_Report_01_07_2021_10_00_12_544_AM_42.zip
8/1/2021 10:00 AM 22642 OpsCenter_All_Master_Server_Backup_Report_01_08_2021_10_00_28_384_AM_25.zip
9/1/2021 10:00 AM 19426 OpsCenter_All_Master_Server_Backup_Report_01_09_2021_10_00_43_851_AM_70.zip
10/1/2021 10:01 AM 19149 OpsCenter_All_Master_Server_Backup_Report_01_10_2021_10_01_00_422_AM_7.zip
11/1/2021 10:00 AM 19638 OpsCenter_All_Master_Server_Backup_Report_01_11_2021_10_00_15_326_AM_20.zip
12/1/2021 11:00 AM 19375 OpsCenter_All_Master_Server_Backup_Report_01_12_2021_10_00_29_943_AM_13.zip
1/2/2022 11:00 AM 22281 OpsCenter_All_Master_Server_Backup_Report_02_01_2022_10_00_45_803_AM_37.zip
2/2/2022 11:00 AM 19435 OpsCenter_All_Master_Server_Backup_Report_02_02_2022_10_00_05_577_AM_71.zip
3/2/2022 11:00 AM 19380 OpsCenter_All_Master_Server_Backup_Report_02_03_2022_10_00_24_973_AM_90.zip
4/2/2021 10:00 AM 21411 OpsCenter_All_Master_Server_Backup_Report_02_04_2021_10_00_03_069_AM_56.zip
Now, I need to get the contents from this page in a structured format. I am using requests module but the data is highly un-structured and difficult to parse. The code is as below..
req = requests.get(url)
print (req.content.decode('utf-8'))
Output is like :
<pre>[To Parent Directory]<br><br> 3/4/2021 6:09 AM <dir> All_Master<br> 3/4/2021 6:09 AM <dir> Hartland<br> 3/4/2021 6:09 AM <dir> Hauppauge<br> 3/4/2021 6:09 AM <dir> Hazelwood<br> 2/15/2019 7:41 AM 58224 NetBackup Retention and Full Backup Occupancy.xlsx<br> 1/1/2022 11:00 AM 23959 OpsCenter_All_Master_Server_Backup_Report_01_01_2022_10_00_45_259_AM_49.zip<br> 2/1/2022 11:00 AM 18989 OpsCenter_All_Master_Server_Backup_Report_01_02_2022_10_00_04_813_AM_4.zip<br> 3/1/2022 11:00 AM 18969 OpsCenter_All_Master_Server_Backup_Report_01_03_2022_10_00_24_664_AM_17.zip<br> 4/1/2021 10:00 AM 21709 OpsCenter_All_Master_Server_Backup_Report_01_04_2021_10_00_02_266_AM_31.zip<br> 5/1/2021 10:00 AM 27491 OpsCenter_All_Master_Server_Backup_Report_01_05_2021_10_00_27_655_AM_11.zip<br> 6/1/2021 10:00 AM 21260 OpsCenter_All_Master_Server_Backup_Report_01_06_2021_10_00_54_053_AM_19.zip<br> 7/1/2021 10:00 AM 19898 OpsCenter_All_Master_Server_Backup_Report_01_07_2021_10_00_12_544_AM_42.zip<br> 8/1/2021 10:00 AM 22642 OpsCenter_All_Master_Server_Backup_Report_01_08_2021_10_00_28_384_AM_25.zip<br> 9/1/2021 10:00 AM 19426 OpsCenter_All_Master_Server_Backup_Report_01_09_2021_10_00_43_851_AM_70.zip<br> 10/1/2021 10:01 AM 19149 OpsCenter_All_Master_Server_Backup_Report_01_10_2021_10_01_00_422_AM_7.zip<br> 11/1/2021 10:00 AM 19638 OpsCenter_All_Master_Server_Backup_Report_01_11_2021_10_00_15_326_AM_20.zip<br> 12/1/2021 11:00 AM 19375 OpsCenter_All_Master_Server_Backup_Report_01_12_2021_10_00_29_943_AM_13.zip<br> 1/2/2022 11:00 AM 22281 OpsCenter_All_Master_Server_Backup_Report_02_01_2022_10_00_45_803_AM_37.zip<br> 2/2/2022 11:00 AM 19435 OpsCenter_All_Master_Server_Backup_Report_02_02_2022_10_00_05_577_AM_71.zip<br> 3/2/2022 11:00 AM 19380 OpsCenter_All_Master_Server_Backup_Report_02_03_2022_10_00_24_973_AM_90.zip<br> 4/2/2021 10:00 AM 21411 OpsCenter_All_Master_Server_Backup_Report_02_04_2021_10_00_03_069_AM_56.zip<br> 5/2/2021 10:00 AM 24191 OpsCenter_All_Master_Server_Backup_Report_02_05_2021_10_00_28_556_AM_14.zip<br> 6/2/2021 10:00 AM 21675 OpsCenter_All_Master_Server_Backup_Report_02_06_2021_10_00_54_962_AM_73.zip<br> 7/2/2021 10:00 AM 19954 OpsCenter_All_Master_Server_Backup_Report_02_07_2021_10_00_13_058_AM_31.zip<br> 8/2/2021 10:00 AM 21085 OpsCenter_All_Master_Server_Backup_Report_02_08_2021_10_00_28_778_AM_79.zip<br> 9/2/2021 10:00 AM 19691 OpsCenter_All_Master_Server_Backup_Report_02_09_2021_10_00_44_294_AM_5.zip<br> 10/2/2021 10:01 AM 23477 OpsCenter_All_Master_Server_Backup_Report_02_10_2021_10_01_00_793_AM_9.zip<br> 11/2/2021 10:00 AM 2
This is very unstructured.
Kindly suggest a way to make this content more readable so it is easy to parse the data...

finding regex pattern to separated unformatted comma separated values in the column of a dataframe in python

hi I've to preprocess a column that has comma separated values and i can't apply .split(',\s*') because there are places where commas and spaces shouldn't be separate so therefore i'm looking for a regex pattern.
column:
0 12noon to 3:30pm, 6:30pm to 11:30pm (Mon-Sun)
1 11 AM to 11 PM
2 11:30 AM to 4:30 PM, 6:30 PM to 11 PM
3 12 Noon to 2 AM
4 12noon to 11pm (Mon, Tue, Wed, Thu, Sun), 12no...
...
100 11 AM to 11 PM
101 10 AM to 10 PM (Mon-Thu), 8 AM to 10:30 PM (Fr...
102 12 Noon to 11 PM
103 8am to 12:30AM (Mon-Sun)
104 11:30 AM to 3 PM, 7 PM to 12 Midnight
what i've tried is
import re
pattern = '([\w+\:*\s*\w*(w{2})*]*\s*to\s*[\w+\:*\s*\w*(w{2})*]*\s*[\([a-zA-Z]*\-*\,*\s*
[a-zA-Z]*\s*\)]*)'
timing = data['timings'].str.lower().str.split(pattern).dropna().to_numpy()
output:
array([list(['12noon to 3:30pm,', ' 6:30pm to 11:30pm (mon-sun)', '']),
list(['11 am to 11 pm']),
list(['11:30 am to 4:30 pm, 6:30 pm to 11 pm']),
list(['12 noon to 2 am']),
list(['12noon to 11pm (mon, tue, wed, thu, sun),', ' 12noon to 12midnight (fri-sat)', '']),
list(['12noon to 3:30pm, 4pm to 6:30pm, 7pm to 11:30pm (mon, tue, wed, thu, sun), 12noon to 3:30pm, 4pm to 6:30pm,', ' 7pm to 12midnight (fri-sat)', '']),
list(['7 am to 10 pm']), list(['12 noon to 12 midnight']),
list(['12 noon to 12 midnight']),
list(['', '10 am to 1 am (mon-thu)', ',', ' 10 am to 1:30 am (fri-sun)', '']),
list(['12 noon to 3:30 pm, 7 pm to 10:30 pm']),
list(['12 noon to 3:30 pm, 6:30 pm to 11:30 pm']),
list(['11:30 am to 1 am']),
list(['', '12noon to 12midnight (mon-sun)', '']),
list(['12 noon to 4:30 pm, 6:30 pm to 11:30 pm']),
list(['11 am to 11 pm']), list(['12 noon to 10:30 pm']),
list(['11:30 am to 1 am']), list(['12 noon to 12 midnight']),
list(['12 noon to 11 pm']),
list(['', '12:30 pm to 10 pm (tue-sun)', ', mon closed']),
list(['11:30 am to 3 pm, 7 pm to 11 pm']),
list(['11am to 11:30pm (mon, tue, wed, thu, sun),', ' 11am to 12midnight (fri-sat)', '']),
list(['10 am to 5 am']),
list(['12 noon to 12 midnight (mon-thu, sun),', ' 12 noon to 1 am (fri-sat)', '']),
list(['', '12noon to 11pm (mon-thu)', ',', '12noon to 11:30pm (fri-sun)', '']),
list(['', '12 noon to 11:30 pm (mon-wed)', ',', ' 12 noon to 1 am (fri-sat)', ',', ' 12 noon to 12 midnight (sun)', ', thu closed']),
list(['12 noon to 4 pm, 6:30 pm to 11:30 pm']),
list(['10 am to 1 am']), list(['4:30 pm to 5:30 am']),
list(['11 am to 12 midnight']),
list(['12noon to 4pm,', ' 7pm to 12midnight (mon-sun)', '']),
list(['11 am to 12 midnight']),
list(['', '6am to 12midnight (mon-sun)', '']),
list(['12 noon to 11 pm']),
list(['12:30 pm to 3:30 pm, 7 pm to 10:40 pm']),
list(['12 noon to 4 pm, 7 pm to 11 pm']),
list(['12noon to 11pm (mon, tue, wed, thu, sun),', ' 12noon to 12midnight (fri-sat)', '']),
list(['12 noon to 10:30 pm']),
list(['', '12noon to 11pm (mon-sun)', '']),
list(['10 am to 10 pm']), list(['10 am to 10 pm']),
list(['7 am to 1 am']), list(['12 noon to 11:30 pm']),
list(['', '12noon to 11:30pm (mon-sun)', '']),
list(['12 noon to 11:30 pm']), list(['12 noon to 11 pm']),
list(['6 am to 10:30 pm']),
list(['11:30 am to 3:30 pm, 6:45 pm to 11:30 pm']),
list(['11:55 am to 4 pm, 7 pm to 11:15 pm']),
list(['12 noon to 11 pm']), list(['11 am to 11 pm']),
list(['12noon to 4:30pm, 6:30pm to 11:30pm (mon, tue, wed, fri, sat), closed (thu),', '12noon to 12midnight (sun)', '']),
list(['12noon to 12midnight (mon, tue, wed, thu, sun),', ' 12noon to 1am (fri-sat)', '']),
list(['8 am to 11:30 pm']),
list(['6:30am to 10:30am, 12:30pm to 3pm,', ' 7pm to 11pm (mon)', ',6:30am to 10:30am, 12:30pm to 3pm,', ' 7:30pm to 11pm (tue-sat)', ',6:30am to 10:30am, 12:30pm to 3:30pm,', ' 7pm to 11pm (sun)', '']),
list(['12 noon to 3 pm, 7 pm to 11:30 pm']),
list(['11:30 am to 1 am']), list(['9 am to 10 pm']),
list(['12 noon to 12 midnight (mon-thu, sun),', ' 12 noon to 1 am (fri-sat)', '']),
list(['', '5pm to 12midnight (mon-sun)', '']),
list(['11 am to 11:30 pm']),
list(['', '11:30am to 11pm (mon-sun)', '']),
list(['12 noon to 10:30 pm']), list(['1 pm to 11 pm']),
list(['11:30 am to 12 midnight']),
list(['12 noon to 12 midnight']),
list(['', '12noon to 12midnight (mon-sun)', '']),
list(['', '12noon to 11pm (mon-sun)', '']),
list(['12 noon to 3 pm, 7 pm to 11 pm']),
list(['12 noon to 3 pm, 7 pm to 11 pm']),
list(['', '11 am to 8 pm (mon-sat)', ', sun closed']),
list(['4 am to 12 midnight']), list(['9 am to 1 am']),
list(['10:30 am to 11 pm']), list(['7 am to 11 pm']),
list(['7 am to 10:30 am, 12:30 pm to 3:30 pm, 7 pm to 11 pm']),
list(['12 noon to 3:30 pm, 7 pm to 11:30 pm']),
list(['12 noon to 3:30 pm, 7 pm to 11 pm']),
list(['12noon to 12midnight (mon, tue, wed, thu, sun),', ' 12noon to 1am (fri-sat)', '']),
list(['', '11am to 11pm (mon-sun)', '']),
list(['6 am to 11:30 pm']), list(['11:30 am to 5 am']),
list(['12:30 pm to 3:30 pm, 7 pm to 11 pm']),
list(['', '6pm to 2am (mon-sun)', '']),......)
but what i want is something like this:
[['6pm to 2am (mon-sun)'], ['12 noon to 12 midnight (mon-thu, sun)'] .....] something like this
i think i've to design a better regex pattern in order to separated these values. so can anyone design a better regex pattern? Thanks in advance:).
Here's my attempt:
import re, pandas
data = pandas.read_excel('C:\\Users\\Administrator\\Desktop\\test.xls')
pattern = '(\d{1,2}(?:\:\d{1,2})? ?(?:\w{2,8}) to \d{1,2}(?:\:\d{1,2})? ?(?:\w{2,8}) ?(?:\(\w{3}(?:[ ,-]{1,3}\w{3}){0,6}\))?)'
re.findall(pattern, data["myData"].str.cat(sep=", "))
With the call to re.findall() my output was:
['12noon to 3:30pm', '6:30pm to 11:30pm (Mon-Sun)', '11 AM to 11 PM', '11:30 AM to 4:30 PM', '6:30 PM to 11 PM', '12 Noon to 2 AM', '11 AM to 11 PM', '10 AM to 10 PM (Mon-Thu)', '8 AM to 10:30 PM (Fri,Sat)', '12 Noon to 11 PM', '8am to 12:30AM (Mon-Sun)', '11:30 AM to 3 PM', '7 PM to 12 Midnight']

Formatting pandas dataframe printing

I have the following pandas dataframe that was converted to string with to_string().
It was printed like this:
S T Q U X A D
02:36 06:00 06:00 06:00 06:30 09:46 07:56
02:37 06:10 06:15 06:15 06:40 09:48 08:00
12:00 11:00 12:00 12:00 07:43 12:00 18:03
13:15 13:00 13:15 13:15 07:50 13:15 18:08
14:00 14:00 14:00 14:00 14:00 19:00
15:15 15:00 14:15 15:15 15:15 19:05
16:15 16:00 15:15 16:15 16:15 20:15
17:15 17:00 17:15 17:15 17:15 20:17
18:15 21:22 21:19 19:55 18:15 20:18
19:15 21:24 21:21 19:58 19:15 20:19
The gaps are due to empty values in the dataframe. I would like to keep the column alignment, perhaps by replacing the empty values with tabs. I would also like to center align the header line.
This wasn't printed in a terminal, but was sent over telegram with the requests post command. I think though, it is just a print formatting problem, independent of the telegram requests library.
The desired output would be like this:
S T Q U X A D
02:36 06:00 06:00 06:00 06:30 09:46 07:56
02:37 06:10 06:15 06:15 06:40 09:48 08:00
12:00 11:00 12:00 12:00 07:43 12:00 18:03
13:15 13:00 13:15 13:15 07:50 13:15 18:08
14:00 14:00 14:00 14:00 14:00 19:00
15:15 15:00 14:15 15:15 15:15 19:05
16:15 16:00 15:15 16:15 16:15 20:15
17:15 17:00 17:15 17:15 17:15 20:17
18:15 21:22 21:19 19:55 18:15 20:18
19:15 21:24 21:21 19:58 19:15 20:19
you can use dataframe style.set_properties to set some of these options like:
df.style.set_properties(**{'text-align': 'center'})
read more here:
https://pandas.pydata.org/docs/reference/api/pandas.io.formats.style.Styler.set_properties.html

Sorting a list of time string ranges

I have the following list which I need to sort in ascending order:
tlist = ['10:10 AM - 10:20 AM', '10:20 AM - 10:30 AM', '10:30 AM - 10:40 AM', '10:40 AM - 10:50 AM', '10:50 AM - 11:00 AM', '11:00 AM - 11:10 AM', '11:10 AM - 11:20 AM', '11:20 AM - 11:30 AM', '11:30 AM - 11:40 AM', '11:40 AM - 11:50 AM', '11:50 AM - 12:00 PM', '12:00 PM - 12:10 PM', '12:10 PM - 12:20 PM', '12:20 PM - 12:30 PM', '12:30 PM - 12:40 PM', '12:40 PM - 12:50 PM', '12:50 PM - 1:00 PM', '1:00 PM - 1:10 PM', '1:10 PM - 1:20 PM', '1:20 PM - 1:30 PM', '1:30 PM - 1:40 PM', '1:40 PM - 1:50 PM', '1:50 PM - 2:00 PM', '2:00 PM - 2:10 PM', '2:10 PM - 2:20 PM', '2:20 PM - 2:30 PM', '2:30 PM - 2:40 PM', '2:40 PM - 2:50 PM', '2:50 PM - 3:00 PM', '3:00 PM - 3:10 PM', '3:10 PM - 3:20 PM', '3:20 PM - 3:30 PM', '3:30 PM - 3:40 PM', '3:40 PM - 3:50 PM', '3:50 PM - 4:00 PM', '4:00 PM - 4:10 PM', '4:10 PM - 4:20 PM', '4:20 PM - 4:30 PM', '4:30 PM - 4:40 PM', '4:40 PM - 4:50 PM', '4:50 PM - 5:00 PM', '5:00 PM - 5:10 PM', '5:10 PM - 5:20 PM', '5:20 PM - 5:30 PM', '5:30 PM - 5:40 PM', '5:40 PM - 5:50 PM', '5:50 PM - 6:00 PM', '6:00 PM - 6:10 PM', '6:10 PM - 6:20 PM', '6:20 PM - 6:30 PM', '6:30 PM - 6:40 PM', '6:40 PM - 6:50 PM', '6:50 PM - 7:00 PM', '7:00 PM - 7:10 PM', '7:10 AM - 7:20 AM', '7:10 PM - 7:20 PM', '7:20 AM - 7:30 AM', '7:20 PM - 7:30 PM', '7:30 AM - 7:40 AM', '7:30 PM - 7:40 PM', '7:40 AM - 7:50 AM', '7:40 PM - 7:50 PM', '7:50 AM - 8:00 AM', '7:50 PM - 8:00 PM', '8:00 AM - 8:10 AM', '8:00 PM - 8:10 PM', '8:10 AM - 8:20 AM', '8:10 PM - 8:20 PM', '8:20 AM - 8:30 AM', '8:20 PM - 8:30 PM', '8:30 AM - 8:40 AM', '8:30 PM - 8:40 PM', '8:40 AM - 8:50 AM', '8:40 PM - 8:50 PM', '8:50 AM - 9:00 AM', '8:50 PM - 9:00 PM', '9:00 AM - 9:10 AM', '9:00 PM - 9:10 PM', '9:10 AM - 9:20 AM', '9:10 PM - 9:20 PM', '9:20 AM - 9:30 AM', '9:20 PM - 9:30 PM', '9:30 AM - 9:40 AM', '9:40 AM - 9:50 AM', '9:50 AM - 10:00 AM']
While attempting to do that, I had written an iterator to list each time string as a time object, but failing in conversion.
import time
tlist = ['10:10 AM - 10:20 AM', '10:20 AM - 10:30 AM', '10:30 AM - 10:40 AM', '10:40 AM - 10:50 AM', '10:50 AM - 11:00 AM', '11:00 AM - 11:10 AM', '11:10 AM - 11:20 AM', '11:20 AM - 11:30 AM', '11:30 AM - 11:40 AM', '11:40 AM - 11:50 AM', '11:50 AM - 12:00 PM', '12:00 PM - 12:10 PM', '12:10 PM - 12:20 PM', '12:20 PM - 12:30 PM', '12:30 PM - 12:40 PM', '12:40 PM - 12:50 PM', '12:50 PM - 1:00 PM', '1:00 PM - 1:10 PM', '1:10 PM - 1:20 PM', '1:20 PM - 1:30 PM', '1:30 PM - 1:40 PM', '1:40 PM - 1:50 PM', '1:50 PM - 2:00 PM', '2:00 PM - 2:10 PM', '2:10 PM - 2:20 PM', '2:20 PM - 2:30 PM', '2:30 PM - 2:40 PM', '2:40 PM - 2:50 PM', '2:50 PM - 3:00 PM', '3:00 PM - 3:10 PM', '3:10 PM - 3:20 PM', '3:20 PM - 3:30 PM', '3:30 PM - 3:40 PM', '3:40 PM - 3:50 PM', '3:50 PM - 4:00 PM', '4:00 PM - 4:10 PM', '4:10 PM - 4:20 PM', '4:20 PM - 4:30 PM', '4:30 PM - 4:40 PM', '4:40 PM - 4:50 PM', '4:50 PM - 5:00 PM', '5:00 PM - 5:10 PM', '5:10 PM - 5:20 PM', '5:20 PM - 5:30 PM', '5:30 PM - 5:40 PM', '5:40 PM - 5:50 PM', '5:50 PM - 6:00 PM', '6:00 PM - 6:10 PM', '6:10 PM - 6:20 PM', '6:20 PM - 6:30 PM', '6:30 PM - 6:40 PM', '6:40 PM - 6:50 PM', '6:50 PM - 7:00 PM', '7:00 PM - 7:10 PM', '7:10 AM - 7:20 AM', '7:10 PM - 7:20 PM', '7:20 AM - 7:30 AM', '7:20 PM - 7:30 PM', '7:30 AM - 7:40 AM', '7:30 PM - 7:40 PM', '7:40 AM - 7:50 AM', '7:40 PM - 7:50 PM', '7:50 AM - 8:00 AM', '7:50 PM - 8:00 PM', '8:00 AM - 8:10 AM', '8:00 PM - 8:10 PM', '8:10 AM - 8:20 AM', '8:10 PM - 8:20 PM', '8:20 AM - 8:30 AM', '8:20 PM - 8:30 PM', '8:30 AM - 8:40 AM', '8:30 PM - 8:40 PM', '8:40 AM - 8:50 AM', '8:40 PM - 8:50 PM', '8:50 AM - 9:00 AM', '8:50 PM - 9:00 PM', '9:00 AM - 9:10 AM', '9:00 PM - 9:10 PM', '9:10 AM - 9:20 AM', '9:10 PM - 9:20 PM', '9:20 AM - 9:30 AM', '9:20 PM - 9:30 PM', '9:30 AM - 9:40 AM', '9:40 AM - 9:50 AM', '9:50 AM - 10:00 AM']
for t in tlist:
f = t.split('-')[0]
print(f)
ft = time.strptime(f, "%I:%M %p")
print(f, ft)
I'm getting an error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-19-0a2d8195df22> in <module>()
4 f = t.split('-')[0]
5 print(f)
----> 6 ft = time.strptime(f, "%I:%M %p")
7 print(f, ft)
/usr/lib/python3.6/_strptime.py in _strptime_time(data_string, format)
557 """Return a time struct based on the input string and the
558 format string."""
--> 559 tt = _strptime(data_string, format)[0]
560 return time.struct_time(tt[:time._STRUCT_TM_ITEMS])
561
/usr/lib/python3.6/_strptime.py in _strptime(data_string, format)
363 if len(data_string) != found.end():
364 raise ValueError("unconverted data remains: %s" %
--> 365 data_string[found.end():])
366
367 iso_year = year = None
ValueError: unconverted data remains:
How can I fix this error? Is there an easier technique of sorting these other than the tedious method of looping over the list and transferring to an intermediary list?
By doing
f = t.split('-')[0]
ft = time.strptime(f, "%I:%M %p")
you end up with a space before and after each date string (eg '10:10 AM - 10:20 AM' becomes '10:10 AM ' and ' 10:20 AM').
This is also what the error message is saying:
ValueError: unconverted data remains:
strptime tried to apply the format %I:%M %p to f, but it got a leftover whitespace it did not know what to do with.
The solution is to either
split on ' - ': f = t.split(' - ')[0]
or
use strip (f = t.split('-')[0].strip()) (probably the better solution as it is a bit more generic)
You could also include the whitespace into the format (time.strptime(f, "%I:%M %p ")) but this will be a de-facto fix just waiting to break again in the future.
Change
f = t.split('-')[0]
To
f = t.split('-')[0].strip()
After split('-'), you will get 2 value exp: '10:10 AM ' and ' 10:20 AM'. So, It need to remove space in these value.
Using sorted
Ex:
import time
tlist = ['10:10 AM - 10:20 AM', '10:20 AM - 10:30 AM', '10:30 AM - 10:40 AM', '10:40 AM - 10:50 AM', '10:50 AM - 11:00 AM', '11:00 AM - 11:10 AM', '11:10 AM - 11:20 AM', '11:20 AM - 11:30 AM', '11:30 AM - 11:40 AM', '11:40 AM - 11:50 AM', '11:50 AM - 12:00 PM', '12:00 PM - 12:10 PM', '12:10 PM - 12:20 PM', '12:20 PM - 12:30 PM', '12:30 PM - 12:40 PM', '12:40 PM - 12:50 PM', '12:50 PM - 1:00 PM', '1:00 PM - 1:10 PM', '1:10 PM - 1:20 PM', '1:20 PM - 1:30 PM', '1:30 PM - 1:40 PM', '1:40 PM - 1:50 PM', '1:50 PM - 2:00 PM', '2:00 PM - 2:10 PM', '2:10 PM - 2:20 PM', '2:20 PM - 2:30 PM', '2:30 PM - 2:40 PM', '2:40 PM - 2:50 PM', '2:50 PM - 3:00 PM', '3:00 PM - 3:10 PM', '3:10 PM - 3:20 PM', '3:20 PM - 3:30 PM', '3:30 PM - 3:40 PM', '3:40 PM - 3:50 PM', '3:50 PM - 4:00 PM', '4:00 PM - 4:10 PM', '4:10 PM - 4:20 PM', '4:20 PM - 4:30 PM', '4:30 PM - 4:40 PM', '4:40 PM - 4:50 PM', '4:50 PM - 5:00 PM', '5:00 PM - 5:10 PM', '5:10 PM - 5:20 PM', '5:20 PM - 5:30 PM', '5:30 PM - 5:40 PM', '5:40 PM - 5:50 PM', '5:50 PM - 6:00 PM', '6:00 PM - 6:10 PM', '6:10 PM - 6:20 PM', '6:20 PM - 6:30 PM', '6:30 PM - 6:40 PM', '6:40 PM - 6:50 PM', '6:50 PM - 7:00 PM', '7:00 PM - 7:10 PM', '7:10 AM - 7:20 AM', '7:10 PM - 7:20 PM', '7:20 AM - 7:30 AM', '7:20 PM - 7:30 PM', '7:30 AM - 7:40 AM', '7:30 PM - 7:40 PM', '7:40 AM - 7:50 AM', '7:40 PM - 7:50 PM', '7:50 AM - 8:00 AM', '7:50 PM - 8:00 PM', '8:00 AM - 8:10 AM', '8:00 PM - 8:10 PM', '8:10 AM - 8:20 AM', '8:10 PM - 8:20 PM', '8:20 AM - 8:30 AM', '8:20 PM - 8:30 PM', '8:30 AM - 8:40 AM', '8:30 PM - 8:40 PM', '8:40 AM - 8:50 AM', '8:40 PM - 8:50 PM', '8:50 AM - 9:00 AM', '8:50 PM - 9:00 PM', '9:00 AM - 9:10 AM', '9:00 PM - 9:10 PM', '9:10 AM - 9:20 AM', '9:10 PM - 9:20 PM', '9:20 AM - 9:30 AM', '9:20 PM - 9:30 PM', '9:30 AM - 9:40 AM', '9:40 AM - 9:50 AM', '9:50 AM - 10:00 AM']
print(sorted(tlist, key=lambda x: time.strptime(x.split("-")[0].strip(), "%I:%M %p")))
Output:
['7:10 AM - 7:20 AM', '7:20 AM - 7:30 AM', '7:30 AM - 7:40 AM', '7:40 AM - 7:50 AM', '7:50 AM - 8:00 AM', '8:00 AM - 8:10 AM', '8:10 AM - 8:20 AM', '8:20 AM - 8:30 AM', '8:30 AM - 8:40 AM', '8:40 AM - 8:50 AM', '8:50 AM - 9:00 AM', '9:00 AM - 9:10 AM', '9:10 AM - 9:20 AM', '9:20 AM - 9:30 AM', '9:30 AM - 9:40 AM', '9:40 AM - 9:50 AM', '9:50 AM - 10:00 AM', '10:10 AM - 10:20 AM', '10:20 AM - 10:30 AM', '10:30 AM - 10:40 AM', '10:40 AM - 10:50 AM', '10:50 AM - 11:00 AM', '11:00 AM - 11:10 AM', '11:10 AM - 11:20 AM', '11:20 AM - 11:30 AM', '11:30 AM - 11:40 AM', '11:40 AM - 11:50 AM', '11:50 AM - 12:00 PM', '12:00 PM - 12:10 PM', '12:10 PM - 12:20 PM', '12:20 PM - 12:30 PM', '12:30 PM - 12:40 PM', '12:40 PM - 12:50 PM', '12:50 PM - 1:00 PM', '1:00 PM - 1:10 PM', '1:10 PM - 1:20 PM', '1:20 PM - 1:30 PM', '1:30 PM - 1:40 PM', '1:40 PM - 1:50 PM', '1:50 PM - 2:00 PM', '2:00 PM - 2:10 PM', '2:10 PM - 2:20 PM', '2:20 PM - 2:30 PM', '2:30 PM - 2:40 PM', '2:40 PM - 2:50 PM', '2:50 PM - 3:00 PM', '3:00 PM - 3:10 PM', '3:10 PM - 3:20 PM', '3:20 PM - 3:30 PM', '3:30 PM - 3:40 PM', '3:40 PM - 3:50 PM', '3:50 PM - 4:00 PM', '4:00 PM - 4:10 PM', '4:10 PM - 4:20 PM', '4:20 PM - 4:30 PM', '4:30 PM - 4:40 PM', '4:40 PM - 4:50 PM', '4:50 PM - 5:00 PM', '5:00 PM - 5:10 PM', '5:10 PM - 5:20 PM', '5:20 PM - 5:30 PM', '5:30 PM - 5:40 PM', '5:40 PM - 5:50 PM', '5:50 PM - 6:00 PM', '6:00 PM - 6:10 PM', '6:10 PM - 6:20 PM', '6:20 PM - 6:30 PM', '6:30 PM - 6:40 PM', '6:40 PM - 6:50 PM', '6:50 PM - 7:00 PM', '7:00 PM - 7:10 PM', '7:10 PM - 7:20 PM', '7:20 PM - 7:30 PM', '7:30 PM - 7:40 PM', '7:40 PM - 7:50 PM', '7:50 PM - 8:00 PM', '8:00 PM - 8:10 PM', '8:10 PM - 8:20 PM', '8:20 PM - 8:30 PM', '8:30 PM - 8:40 PM', '8:40 PM - 8:50 PM', '8:50 PM - 9:00 PM', '9:00 PM - 9:10 PM', '9:10 PM - 9:20 PM', '9:20 PM - 9:30 PM']

Categories