Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在每个月的最后一个数据之后打印一些文本?_Python_Sqlite - Fatal编程技术网

Python 如何在每个月的最后一个数据之后打印一些文本?

Python 如何在每个月的最后一个数据之后打印一些文本?,python,sqlite,Python,Sqlite,我有一个表格,在我的表格数据中,2019年11月有三个日期,2019年12月有两个日期,2020年1月有一个日期。我想在每个月的最后一天打印月底。在我的代码中,它在每个月的第一天之后打印 我从代码中获得以下输出: 2019-11-2 End of the month 2019-11-28 2019-11-30 2019-12-25 End of the month 2019-12-28 2020-01-30 End of the month 我希望输出如下所示: 2019-11-2 2019-

我有一个表格,在我的表格数据中,2019年11月有三个日期,2019年12月有两个日期,2020年1月有一个日期。我想在每个月的最后一天打印月底。在我的代码中,它在每个月的第一天之后打印

我从代码中获得以下输出:

2019-11-2
End of the month
2019-11-28
2019-11-30
2019-12-25
End of the month
2019-12-28
2020-01-30
End of the month
我希望输出如下所示:

2019-11-2
2019-11-28
2019-11-30
End of the month
2019-12-25
2019-12-28
End of the month
2020-01-30
End of the month
from tkinter import *
import sqlite3
from datetime import datetime

conn = sqlite3.connect("mydatabase.db")
cursor = conn.cursor()

table = cursor.execute("""
CREATE TABLE IF NOT EXISTS product_transfer(`date_of_transfer` DATE);
""")

cursor.execute("""
INSERT INTO product_transfer(`date_of_transfer`)VALUES
  ('2019-11-2'),
  ('2019-11-28'),
  ('2019-11-30'),
  ('2019-12-25'),
  ('2019-12-28'),
  ('2020-01-30');
""")

information = cursor.execute("SELECT *FROM product_transfer").fetchall()
printed = set()
for i in information:
    b = (i[0])
    print(b)
    date = datetime.strptime(b, "%Y-%m-%d")
    if str(date.month) + str(date.year) not in printed:
        printed.add(str(date.month) + str(date.year))
        print('End of the month')


我的代码如下:

2019-11-2
2019-11-28
2019-11-30
End of the month
2019-12-25
2019-12-28
End of the month
2020-01-30
End of the month
from tkinter import *
import sqlite3
from datetime import datetime

conn = sqlite3.connect("mydatabase.db")
cursor = conn.cursor()

table = cursor.execute("""
CREATE TABLE IF NOT EXISTS product_transfer(`date_of_transfer` DATE);
""")

cursor.execute("""
INSERT INTO product_transfer(`date_of_transfer`)VALUES
  ('2019-11-2'),
  ('2019-11-28'),
  ('2019-11-30'),
  ('2019-12-25'),
  ('2019-12-28'),
  ('2020-01-30');
""")

information = cursor.execute("SELECT *FROM product_transfer").fetchall()
printed = set()
for i in information:
    b = (i[0])
    print(b)
    date = datetime.strptime(b, "%Y-%m-%d")
    if str(date.month) + str(date.year) not in printed:
        printed.add(str(date.month) + str(date.year))
        print('End of the month')



试着这样做:

prevMonth = 0

for i in information:
    b = (i[0])
    month = int(b.split('-')[1]) #assuming that b is a string
    
    date = datetime.strptime(b, "%Y-%m-%d")
    if str(date.month) + str(date.year) not in printed:
        printed.add(str(date.month) + str(date.year))
        if prevMonth and month != prevMonth:
           print('End of the month')
    print(b)
    prevMonth = month

您只需处理此处未打印的最后一个案例。

将订单按日期添加到您的选择中,它将确保数据按日期排序date@balderman我按转账日期添加订单,但如何获得预期结果?我想在每个月的最后一天打印月末,但在您的代码中,它不是在上个月的最后一天打印的。我在帖子末尾提到:你必须检查是否需要避免连续打印两次句子,如果需要,在循环结束后再打印一次句子。好的,它有效,谢谢!通常,我们用“向上投票”按钮标记一个有用的答案。谢谢