【问题标题】:TypeError: %d format: a number is required, not getset_descriptor类型错误:%d 格式:需要一个数字,而不是 getset_descriptor
【发布时间】:2015-02-04 02:35:28
【问题描述】:

我正在尝试编写一个脚本来找出当前日期和时间,然后创建一个基于此名称的文件夹。 当我尝试运行我的代码时出现此错误:

TypeError: %d 格式:需要一个数字,而不是 getset_descriptor

这是我的代码:

import os
import time
#import RPi.GPIO as GPIO
import logging
import sys
from datetime import datetime
d = datetime
initYear = "%04d" % (d.year) 
initMonth = "%02d" % (d.month) 
initDate = "%02d" % (d.day)
initHour = "%02d" % (d.hour)
initMins = "%02d" % (d.minute)ion where you wish to save files. Set to HOME as default.
# If you run a local web server on Apache you could set this to /var/www/ to make them
# accessible via web browser.
folderToSave = "/home/timelapse/timelapse_" + str(initYear) + str(initMonth) + str(initDate) +        str(initHour) + str(initMins)
#os.mkdir(folderToSave)
# Set the initial serial for saved images to 1
fileSerial = 1
a = 'timefile'

# Run a WHILE Loop of infinity
while True:
     if os.path.isfile(a) == False:
    # Set FileSerialNumber to 000X using four digits
    fileSerialNumber = "%04d" % (fileSerial)
    # Capture the CURRENT time (not start time as set above) to insert into each capture image filename
    hour = "%02d" % (d.hour)
    mins = "%02d" % (d.minute)
    # Define the size of the image you wish to capture.
    imgWidth = 800 # Max = 2592
    imgHeight = 600 # Max = 1944
    print " ====================================== Saving file at " + hour + ":" + mins
    # Capture the image using raspistill. Set to capture with added sharpening, auto white balance and average metering mode
    # Change these settings where you see fit and to suit the conditions you are using the camera in
    os.system("raspistill -w " + str(imgWidth) + " -h " + str(imgHeight) + " -o " + str(folderToSave) + "/" + str(fileSerialNumber) + "_" + str(hour) + str(mins) +  ".jpg  -sh 40 -awb auto -mm average -v")
    # Increment the fileSerial
    fileSerial += 1
    # Wait 10 minutes before next capture
    time.sleep(600)
else:
    os.remove(time.txt)
os.system("sudo shutdown -h -P now")
break
print "Quitting now."
sys.exit(0)

我认为这里的代码有错误:

initYear = "%04d"

错误似乎与“%04d”部分有关。任何建议或帮助将不胜感激。提前致谢。

【问题讨论】:

  • 你把什么放入这个格式字符串?占位符规范不是问题
  • 这不是你的错误所在。请提供回溯。
  • @MartijnPieters 我确实从“%04d”位的末尾删除了“% (d.year)”,希望它能起作用,但没有帮助。
  • @Gerrat 这是回溯:回溯(最近一次调用最后一次):文件“prog.py”,第 28 行,在 类型错误:%d 格式:需要一个数字,而不是 getset_descriptor
  • @TomD: d.year 是一个描述符,因为d 是对datetime 类型的引用。

标签: python python-2.7 datetime syntax-error typeerror


【解决方案1】:

您没有在此处创建datetime 实例:

d = datetime

这只是datetime 类型的新引用d.yeard.month 等属性是描述符,而不是可以插入的值:

>>> from datetime import datetime
>>> datetime.year
<attribute 'year' of 'datetime.date' objects>
>>> type(datetime.year)
<type 'getset_descriptor'>
>>> '%04d' % datetime.year
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not getset_descriptor

如果你想知道当前的时间戳,你需要调用datetime.now()

d = datetime.now()

有更好的方法在字符串中使用日期时间值。您可以使用datetime.strftime() method 为您的日期生成字符串格式:

formatted = d.strftime('%Y%m%d%H%M')
folderToSave = "/home/timelapse/timelapse_" + formatted

或者您可以在 str.format() 插值中使用相同的格式代码:

folderToSave = "/home/timelapse/timelapse_{:%Y%m%d%H%M}".format(d)

【讨论】:

    【解决方案2】:

    您的问题是您对日期时间的使用。 datetime 是一个类,而不是一个方法。

    要获取当前日期,请使用:

    d = datetime.now()
    

    您似乎正在尝试格式化各种日期组件。 但是,要引用它们,您需要使用这种格式:

    inityear = str(d.year)
    

    【讨论】:

      猜你喜欢
      • 2020-09-24
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      • 2015-01-31
      • 2021-07-08
      • 2017-11-17
      • 1970-01-01
      • 2017-12-28
      相关资源
      最近更新 更多