【问题标题】:Issues with If Else statementsIf Else 语句的问题
【发布时间】:2023-12-31 10:10:01
【问题描述】:

我正在从头开始学习 Python,因为我没有太多的编码背景。在这个特定的练习中,我的任务是获取一个文本文件,删除空格和逗号,然后将其打印为七行(我已经完成了)。现在,我完成了任务,我需要显示真实的,在每行上的单个 int 之前增加时间,同时还在读取“空白”的行中添加一天。

我尝试了几种方法,但似乎无法同时满足这两个标准。这是我写的代码:

from datetime import datetime
from datetime import timedelta

with open("C:\Users\curnutte\Desktop\Assignment\Python Scripts\Python example\RandomFile.txt", "r") as inp:
    with open("C:\Users\curnutte\Desktop\Assignment\Python Scripts\Python example\RandomFileOutput.txt", "w") as outp:

    clock = datetime.now()

    for line in inp.readlines():
        total = 0
        line = line.strip()
        parts = line.split(",")
        for part in parts:

            try:
                num = int(part)
                total += num

            except ValueError:
                total = (" ".join(parts))
                break

    #for line in inp:

    if total == int:
        total_time = clock + timedelta(seconds = 1)
        print (clock + timedelta (seconds = 1))
    else:
        total_time = clock + timedelta(days = 1)
        print (clock + timedelta (days = 1))

    outp.write("%s: " % total_time)

    outp.write('{}\n'.format(total))

这里是“随机文件:”

1,2
2,3
3,4
4,5
blank,blank
5,6
6,7

使用我提供的代码,这是我收到的“RandomFileOutput”:

2016-06-28 13:47:56.106000:13

直到我添加最后一个 if,else 语句,我收到的输出是:

2016-06-28 13:51:19.709000: 3
2016-06-28 13:51:19.709000: 5
2016-06-28 13:51:19.709000: 7
2016-06-28 13:51:19.709000: 9
2016-06-28 13:51:19.709000: blank blank
2016-06-28 13:51:19.709000: 11
2016-06-28 13:51:19.709000: 13

谁能解释我做错了什么?

【问题讨论】:

  • total == int 应该做什么?我没有看到你在哪里分配(隐藏)内置 int 函数,我不明白为什么你的 total 会与它相等?
  • if total == int?您是否要测试总计是否大于 0?
  • 如果 type(total) 是 int 应该是?,否则它是一个字符串的部分?
  • 好吧,我正在尝试使用它来调用文本文件每一行中的数字。老实说,我真的不确定自己在做什么。
  • 好吧,你应该使用isinstance(total, int),或者最好还是将所有内容都包含在tryexcept 块中,except 块将包含当前 else 代码块中的所有内容

标签: python datetime


【解决方案1】:

我认为你的缩进是错误的,你应该检查总数的类型,而不是总数==int:

from datetime import datetime
from datetime import timedelta

with open("RandomFile.txt", "r") as inp:
    with open("RandomFileOutput.txt", "w") as outp:

        clock = datetime.now()

        for i, line in enumerate(inp.readlines()):
            total = 0
            line = line.strip()
#             print(line)
            parts = line.split(",")
            for part in parts:

                try:
                    num = int(part)
                    total += num

                except ValueError:
                    total = (" ".join(parts))
                    break

            #for line in inp:
            print(type(total))
            if type(total) == int:
                total_time = clock + timedelta(seconds = 1)
                print (clock + timedelta(seconds = 1))
            else:
                total_time = clock + timedelta(days = 1)
                print (clock + timedelta(days = 1))
            outp.write("%s: " % total_time)

            outp.write('{}\n'.format(total))

打印:

<class 'int'>
2016-06-28 16:12:11.967791
<class 'int'>
2016-06-28 16:12:11.967791
<class 'int'>
2016-06-28 16:12:11.967791
<class 'int'>
2016-06-28 16:12:11.967791
<class 'str'>
2016-06-29 16:12:10.967791
<class 'int'>
2016-06-28 16:12:11.967791
<class 'int'>
2016-06-28 16:12:11.967791

【讨论】:

  • 非常感谢,文斯!事实证明,在没有指导的情况下学习这种语言很棘手
  • 出于好奇:如果我想继续更改日期(即从 28 更改为 29 后,保留 29)我该怎么做?
  • 使用变量来跟踪for循环范围之外的时间Delta
最近更新 更多