Python Forum
Trying to get the week number from a string of numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to get the week number from a string of numbers
#1
Hi,
I am parsing a json file and the string it returns the date as looks like this:
'2018-03-31T00:19:38Z'

This is ISO 8601 format. I have looked at various libraries to try to parse this and have gotten nothing to work.

I wrote some code that turns that into 2018-03-31. However that is still a string and for the functions I want to use in datetime I need to pass it a number.

This is what my code looks like now

from collections import Counter
import json
import datetime
import dateutil.parser


#List & json file stuffs
mylist = []
with open ('dates.txt') as a:
    data = json.load(a)

#Loop, count & clean
for dat in range(len(data)):
    test = (data[dat].get('commit').get('author').get('date'))
    test2 = test[:-10]
    #test3 = test2.replace("-", ", ")

    #datetime.date(2010, 6, 16).isocalendar()[1]
   # yourdate = dateutil.parser.parse(test, ignoretz=True)
   # new = datetime.date(yourdate)
     mylist.append(test)

#Show me the money
print (mylist)
There is other code commented out from other things I have tried.
Reply
#2
Lookup casting a variable from one type to another https://www.peterbe.com/plog/interesting...-in-python
Reply
#3
Try something like the following code. See https://docs.python.org/3/library/datetime.html

import dateutil.parser

s = '2018-03-31T00:19:38Z'
t = dateutil.parser.parse(s)
print("Default: {}".format(t))
print("Year: {}".format(t.year))
print("Month: {}".format(t.month))
print("Day of Month: {}".format(t.day))
weeknumber = int(t.strftime("%W"))
iweekday = int(t.strftime("%w"))
sdayofweek = t.strftime("%A")
sdayofweekshort = t.strftime("%a")
print("Week Number: {}".format(weeknumber))
print("Day of Week: {}".format(iweekday))
print("Day of Week: {}".format(sdayofweek))
print("Day of Week: {}".format(sdayofweekshort))
Output:
Default: 2018-03-31 00:19:38+00:00 Year: 2018 Month: 3 Day of Month: 31 Week Number: 13 Day of Week: 6 Day of Week: Saturday Day of Week: Sat
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,399 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  String to Number in Python Godjuned 3 1,340 Nov-17-2022, 07:22 AM
Last Post: Godjuned
  number to string Ali_ 1 1,267 Mar-31-2022, 11:22 AM
Last Post: ndc85430
  Convert list of numbers to string of numbers kam_uk 5 2,982 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Convert multiple decimal numbers in string to floats Katy8 6 3,522 Aug-16-2020, 06:06 PM
Last Post: Katy8
  Counting the number of occurrences of characters in a string nsadams87xx 1 1,908 Jun-16-2020, 07:22 PM
Last Post: bowlofred
  Adding string numbers, while loop and exit without input. Jose 11 7,447 Apr-15-2020, 08:34 AM
Last Post: Jose
  Calculating the number of day of the week RbaPhoenix 3 2,478 Mar-27-2020, 09:23 PM
Last Post: Larz60+
  how do I show 12 numbers from a huge number? anabeatriz 38 11,344 Sep-24-2019, 02:29 PM
Last Post: anabeatriz
  hardest week yet: some guidence please raymond2688 25 10,646 Aug-22-2019, 05:23 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020