【问题标题】:Compare a user inputted value to an iterated value in a list将用户输入的值与列表中的迭代值进行比较
【发布时间】:2017-12-11 23:18:27
【问题描述】:

我正在尝试将文件列表(以日期命名)中的值与用户输入的开始和结束日期进行比较,但是在比较值时遍历列表时遇到了一些问题。

代码如下:

import os
import datetime
from tkinter.filedialog import askdirectory
x = askdirectory()
start = input('Enter start date (ddmmyyyy): ')
    end = input('Enter end date (ddmmyyyy): ')
    start = datetime.datetime.strptime(start, "%d%m%Y").strftime("%Y%m%d")
    end = datetime.datetime.strptime(end, "%d%m%Y").strftime("%Y%m%d")
    start = int(start)
    end = int(end)

for files in os.walk(x):
        file = files[2]
        if '2' in file[1]:
            file = [int(i) for i in file]
            print(len(file))
            for i in file:
                if start >= file >= end:
                    fr.file_reader(time,rdata,intensity,files[i])
                    print(files[i])

当我运行它时,我收到以下错误:

TypeError: '>=' not supported between instances of 'int' and 'list'

我尝试将输入转换为整数,尝试将列表本身转换为整数,但这没有帮助。我知道目前它正在将文件作为 if 循环中的整个列表读取,我只希望它读取第 i 个文件并遍历,以便使用我的工作文件阅读器程序读取该文件。我不知道如何实现这一点。

这是我的文件列表中前 10 个文件的样子。全长312条,这里就不一一复制了。

['20151123000103', '20151123220540', '20151124000043', '20151124003712', '20151125000055', '20151125070850', '20151126000101', '20151126000204', '20151126000330', '20151126000513']

如果我能得到任何帮助,我将不胜感激:)

【问题讨论】:

    标签: string list python-3.x list-comprehension typeerror


    【解决方案1】:

    所以我修复了它,错误本身是通过将“file”替换为“i”来修复的。知道这将是一件小而简单的事情。

    【讨论】:

      猜你喜欢
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 2016-03-16
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多