【问题标题】:Timestamp url with function带函数的时间戳url
【发布时间】:2016-02-29 14:23:57
【问题描述】:

我正在尝试将我的函数插入一些我从“日期时间”url API 中获得的“int”。 首先我从 API 导入 UNIX 时间戳,我得到的数据不是一个干净的数字,所以我需要用强制转换函数来划分它,所以我只会得到“数字”,我得到一个“1382310549”的 UNIX 并转换它到一个常规日期,然后我只要求“年”,我得到“2013”​​,问题是我的功能给我“无”而不是给我“2013”​​相等的东西,那就是“哇“…… 它看起来函数无法理解什么是 (value.strftime('%Y')...

import urllib.request
import datetime
response3 = urllib.request.urlopen("https://blockchain.info/q/addressfirstseen/1LuckyR1fFHEsXYyx5QK4UFzv3PEAepPMK").read()

def type_conversion(val, to_type, default=None):
    try:
        return to_type(val)
    except ValueError:
        return default

def FirstTransaction(c):
        if c == 2013 :
            return  ('wow')
        elif c == 2010 :
            return  ('yeaaaa')

timestamp = (type_conversion(response3, int))
value = datetime.datetime.fromtimestamp(timestamp)
print(FirstTransaction(value.strftime('%Y')))

Tnx 伙计们!

【问题讨论】:

    标签: python api http python-3.x url


    【解决方案1】:

    value.strftime('%Y') 产生一个类似"2013" 的字符串。所以你需要在FirstTransaction中比较c == "2013"c == "2010"

    顺便说一句:如果 c 不在 2013 年或 2010 年之间,您可能希望返回一些默认字符串。目前,您隐式返回 None

    【讨论】:

      【解决方案2】:

      value.strftime('%Y') 给你一个字符串而不是一个 int。尝试任一:

      print(FirstTransaction(int(value.strftime('%Y'))))
      

      或:

      def FirstTransaction(c):
          if c == "2013" :
              return  ('wow')
          elif c == "2010" :
              return  ('yeaaaa')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-10
        • 2012-05-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多