Python 中的日期范围错误

基数万能

在 python 中编写了一个片段,从数据库中提取数据并将它们显示在 python shell 上,但是我犯了一些愚蠢的错误,因为我只得到一个日期而不是范围内的日期。我的代码如下:

# open a database connection
connection = MySQLdb.connect ()

# prepare a cursor object using cursor() method
cursor = connection.cursor ()

# execute the SQL query using execute() method.
cursor.execute ()

data = cursor.fetchall()
ddt =  sorted(set(data))

def dte():
  for row in ddt:
   date = (' '.join(map(str,row)))
  return date

print dte()

这是我得到的输出:2017-11-09 但相反,我应该在一个范围内获得多个日期,例如:2017-06-17,2017-06-18 等等。

乌杜斯萨马德

你只是循环每个日期什么都不做,最后,你返回最后一个。

# open a database connection
connection = MySQLdb.connect ()

# prepare a cursor object using cursor() method
cursor = connection.cursor ()

# execute the SQL query using execute() method.
cursor.execute ()

data = cursor.fetchall()
ddt =  sorted(set(data))

def dte():
    dates = [] #Here you need to store the dates in a list or something
    for row in ddt:
        dates.append(' '.join(map(str,row))) #Then append them to the list
    return(dates) #Finally return them EDITED

for i in dte(ddt):
    print(i)

还有一件事,ddt用作论据。从而避免范围问题。

喜欢:

def dte(data):
          dates = [] #Here you need to store the dates in a list or something
          for row in data:
           dates.append(' '.join(map(str,row))) #Then append them to the list
          return(dates) #Finally return them

for i in dte(ddt):
     print(i)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

熊猫python中的日期范围问题

来自分类Dev

在Python中根据当前日期显示日期范围

来自分类Dev

Python中的Dateetry Tkinter日期格式错误

来自分类Dev

Python中的日期验证布尔错误?

来自分类Dev

替换python pandas中的错误日期值

来自分类Dev

Python Pandas:填充日期在多索引中的范围

来自分类Dev

在 Python 中获取日期时间范围之间的小时数

来自分类Dev

python-如何从熊猫python中的日期记录中增加日期范围的值?

来自分类Dev

python中的缩进错误和索引超出范围错误

来自分类Dev

如何在Python中为每个月的特定日期创建日期范围?

来自分类Dev

在 Python 中无需过滤即可在日期范围内查找最大日期

来自分类Dev

python中的列表索引超出范围错误

来自分类Dev

python中的列表索引超出范围错误

来自分类Dev

python:代码中的列表索引超出范围错误

来自分类Dev

在执行范围分箱时处理 Python 中的值错误

来自分类Dev

python中的“IndexError:列表索引超出范围”错误

来自分类Dev

使用Python缩放日期范围

来自分类Dev

python日期范围列表元素

来自分类Dev

Python根据日期范围计算新日期

来自分类Dev

错误的Python日期计算。

来自分类Dev

Python错误控制日期

来自分类Dev

Python - 解析日期错误

来自分类Dev

如何从Python中的日期时间范围中减去星期日

来自分类Dev

读取文件时Python熊猫中的日期解析错误

来自分类Dev

python中datetime模块中日期的位置错误

来自分类Dev

Python OverflowError:数学范围错误

来自分类Dev

Python OverflowError:数学范围错误

来自分类Dev

如何使用python在两列中扩展具有日期范围的数据框?

来自分类Dev

在特定日期范围内的Python代码中的SQL选择查询

Related 相关文章

热门标签

归档