简体   繁体   中英

Mapping for datetime conversion from requests.get

from datetime import datetime

date = "24.03.2021 15:35:20"  # type datetime
birthday = "19.12.1990"  # type date


def convert(data, type):
    if type == "datetime":
        data = datetime.strptime(data, "%d.%m.%Y %H:%M:%S")
        timestamp = datetime.timestamp(data)
        return timestamp
    elif type == "date":
        data = datetime.strptime(data, "%d.%m.%Y")
        timestamp = datetime.timestamp(data)
        return timestamp

    return None


recorded_date = convert(date, "datetime")
recorded_birthday = convert(birthday, "date")

data = {
    "date": {"value": "recorded_date", "type": "datetime"},
    "firstname": {"value": "John", "type": "string"},
    "last_name": {"value": "Doe", "type": "string"},
    "birthday": {"value": "recorded_birthday", "type": "date"},
}

# save data to database with api requests(json) without type like {'date': recorded_date, 'firstname': 'John',...}


# retrive data from database with api requests(json)
newdata = requests.get(url)
print(newdata.json())
# {'date': 1616589320.0, 'firstname': 'John', 'last_name': 'Doe', 'birthday': 661554000.0}

As you can see my data is fine but i need to convert back from timestamp to datetime or string but i have many data so i dont know the fields to convert because i dont have the type anymore i want this output

# {'date': 24.03.2021 15:35:20, 'firstname': 'John', 'last_name': 'Doe', 'birthday': 19.12.1990}

Is there any idea? how to do this? and finally i don't know my data it can be anything so it has to be general someone can add birthday another one can add anniversary it is a large database so i dont want to check all my fields

the problem is dont know the keys of my dictionary it can be anything

You can use datetime.fromtimestamp to parse a timestamp string into datetime. You could rewrite your convert function to flag it as something you want to convert back into datetime. Try the following code:

from datetime import datetime

def convert(data, type):
    if type == "datetime":
        data = datetime.strptime(data, "%d.%m.%Y %H:%M:%S")
        timestamp = datetime.timestamp(data)
        return 'd'+str(timestamp)
    elif type == "date":
        data = datetime.strptime(data, "%d.%m.%Y")
        timestamp = datetime.timestamp(data)
        return 'd'+str(timestamp)

    return None


recorded_date = convert(date, "datetime")
recorded_birthday = convert(birthday, "date")


newdata = requests.get(url)
serialized = newdata.json() # convert to dict
for key in serialized:
    # if it's in format 'd12314151231' ('d' in front of a timestamp)
    if serialized[key][0] == 'd' and serialized[key][1].isnumeric():
        serialized[key] = datetime.fromtimestamp(serialized[key])

This will convert serialized['date'] and serialized['birthday'] into datetime objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM