作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 OSX 上运行 python 脚本以将视频文件 (single_file
) 上传到 YouTube:
# define recording date as date of file modification
# https://developers.google.com/youtube/v3/docs/videos#resource
recordingDate = datetime.fromtimestamp(os.path.getctime(single_file)).isoformat("T")+"Z"
# define video title as file name
filename, file_extension = os.path.splitext(os.path.basename(single_file))
try:
initialize_upload(youtube, args, single_file, title, recordingDate)
except HttpError, e:
print " An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
在某些情况下它运行良好,但在其他情况下 Google 返回以下错误 -
Invalid value for: Invalid format: \"2017-09-22T22:50:55Z\" is malformed at \"Z\"
我应该如何修复它才能从文件中获取正确的日期? YouTube 需要 ISO 8601 (YYYY-MM-DDThh:mm:ss.sZ) 格式的值。
最佳答案
您在问题中分享的链接清楚地说明了格式
The value is specified in ISO 8601 (YYYY-MM-DDThh:mm:ss.sZ) format.
所以你的问题是当微秒信息不可用时,isoformat 将没有微秒。下面的代码显示了差异
>>> current_date = datetime.now()
>>> current_date.isoformat()
'2018-05-20T10:18:26.785085'
>>> current_date.replace(microsecond=0).isoformat()
'2018-05-20T10:18:26'
因此,对于它工作的文件,微秒
将是非零的。所以解决办法很简单
recordingDate = datetime.fromtimestamp(os.path.getctime(single_file)).replace(microsecond=0).isoformat("T")+".0Z"
这将确保微秒始终被截断并稍后设置为 .0
关于python - 如何获取正确格式的日期以供 YouTube 上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50304415/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!