【问题标题】:I am getting the following error"for line in f(list): TypeError: '_io.TextIOWrapper' object is not callable" while running the below python code在运行以下 python 代码时,我收到以下错误“for line in f(list): TypeError: '_io.TextIOWrapper' object is not callable”
【发布时间】:2017-05-27 19:28:31
【问题描述】:
import os,csv,datetime,list

startingtime = input('starting time:')
endtime=input('end time:')

with open('csvfile.csv') as f:
   list=[f,startingtime,endtime]
   for line in f(list):
      print(line)

【问题讨论】:

  • 你想用你的代码实现什么?您想要做什么的示例输入和示例输出是什么?请edit您的问题提供更多信息并使用正确的格式。
  • f 没有定义__call__ 方法,所以你不能做f(list)

标签: python csv date-range python-3.6


【解决方案1】:

您似乎正在尝试使用f(list) 调用f。不要那样做。 f 是文件句柄,而不是函数。此外,避免声明与 list 之类的关键字共享名称的变量。我不确定您对 list 或您的 CSV 文件做了什么,但如果您尝试将开始时间和结束时间写入单行 CSV 文件,这可能会有所帮助。

with open('csvfile.csv', 'w') as f:
   mylist = [startingtime, endtime]
   writer = csv.writer(f)
   writer.writerow(mylist)

教程请看这里:https://docs.python.org/3.6/library/csv.html

【讨论】:

    【解决方案2】:
    【解决方案3】:

    非常抱歉没有清楚地写出问题。下面是我需要访问的类似 csv 文件。在每一行中都会提到时间(第 5 列中的 5:18 到 5:22)。我的问题是,我需要获取输入(开始时间和结束时间)并打印从 5:18 到 5:21 的整行。也就是说,如果我将开始时间设为 5:19,将结束时间设为 5:21。它应该打印从第 2 行到第 4 行的整行。

    aaaaa lshds dslkd dslkd 5:18 ss

    dslkd lsh2d dslkd dslkd 5:19 dd

    lshds lshds lshds lshds 5:20 dd

    aaaaa dslkd dslka dslkd 5:21 dase

    dslkd dslkd dslkd dslkd 5:22 daew

    【讨论】:

      猜你喜欢
      • 2020-08-05
      • 1970-01-01
      • 2022-11-20
      • 2016-03-25
      • 2018-01-26
      • 2014-10-19
      • 2020-11-12
      • 2021-09-15
      • 1970-01-01
      相关资源
      最近更新 更多