我环顾四周,看到了一些我认为适用于我自己的代码的解决方案,但似乎什么也没做。
我正在尝试从 JSON 文件中提取数据并将信息添加到数组中。然后,这些数组将用于将数据输入到我添加到数据库中的几个对象中(我知道,这是非常低效的,但是按照我编写原始脚本的方式,我不必将对象添加到数据库中。我是计划改变这一点)。
对象包含与其关联的日期时间。我使用Python的strptime函数将字符串转换为日期时间对象。
在我的 models.py 中,我有一个日期时间的 DateTimeField。
但是,当我尝试迁移时,出现以下错误:
TypeError: expected string or bytes-like object
Run Code Online (Sandbox Code Playgroud)
这是整个回溯:
Applying interactive_table.0016_auto_20161024_2259...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/andrewho/anaconda/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Users/andrewho/anaconda/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/andrewho/anaconda/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/andrewho/anaconda/lib/python3.5/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/Users/andrewho/anaconda/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/Users/andrewho/anaconda/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, …Run Code Online (Sandbox Code Playgroud) 我正在学习如何使用JSON对象的教程(链接:https://www.youtube.com/watch?v = Y5dU2aGHTZg).当他们运行代码时,他们没有错误,但我做到了.它与不同的Python版本有什么关系吗?
from urllib.request import urlopen
import json
def printResults(data):
theJSON = json.loads(data)
print (theJSON)
def main():
urlData ="http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson"
webUrl = urlopen(urlData)
print(webUrl.getcode())
if (webUrl.getcode()==200):
data = webUrl.read()
printResults(data)
else:
print ("You failed")
main()
Run Code Online (Sandbox Code Playgroud)