小编viG*_*027的帖子

功能测试没有按预期进行(AoC 第 4 天的一部分)

我写了一个函数来检查数据是否正确。要求如下:

byr-(出生年份) - 四位数字;至少 1920 年,最多 2002 年。

iyr (Issue Year) - 四位数字;至少 2010 年,最多 2020 年。

eyr (Expiration Year) - 四位数字;至少 2020 年,最多 2030 年。

def check_byr_iyr_eyr(line):
    statement = True
    if line[:3] == "byr":
        if (len(line[line.index(':')+1:]) != 4 or
        1920 > int(line[line.index(':')+1:]) > 2002 ):
            statement = False
    elif line[:3] == "iyr":
        if (len(line[line.index(':')+1:]) != 4 or
        2010 > int(line[line.index(':')+1:]) > 2020 ):
            statement = False
    elif line[:3] == "eyr":
        if (len(line[line.index(':')+1:]) != 4 or
        2020 > int(line[line.index(':')+1:]) > …
Run Code Online (Sandbox Code Playgroud)

python if-statement function

2
推荐指数
1
解决办法
48
查看次数

从 .txt 文件读取撇号时出现乱码

从 .txt 文件读取行时遇到问题。我的文件包含带有以下单词的句子

\n
\n

没有\xe2\x80\x99t ,可以\xe2\x80\x99t ,没有\xe2\x80\x99t

\n
\n

等等,问题是当我使用read()方法时

\n
\n

\xe2\x80\x99

\n
\n

我有类似的东西:

\n
\n

\xc3\xa2\xe2\x82\xac\xe2\x84\xa2

\n
\n

所以我读到的词hadn\xc3\xa2\xe2\x82\xac\xe2\x84\xa2thadn\xe2\x80\x99t

\n

我的输入:

\n
Love at First Sight\n\nOne <adjective> afternoon, I was walking by the <place> when\naccidentally I bumped into a <adjective> boy.\nAt first I blushed and apologized for bumping into him, but when he flashed his\n<adjective> smile I just couldn\xe2\x80\x99t help falling in love. His\n<adjective> voice telling me that it was ok …
Run Code Online (Sandbox Code Playgroud)

python character-encoding

0
推荐指数
1
解决办法
1075
查看次数