0
我从API接收到一个JSON文件。我将以自定义格式将其写入文件。我的脚本是:当使用F.write()时,为什么我会得到“TypeError:并非在字符串格式化期间转换的所有参数?”?
import logging,json,urllib,time
logging.basicConfig(level=logging.DEBUG)
log_time = time.strftime("%Y-%m-%d %H:%M:%S")
logging.info("程序开始执行,%s",log_time)
logging.info("从远程API下载json,http://172.100.0.21:8080/patch/file/filelist",log_time)
url = "http://172.100.0.21:8080/patch/file/filelist?date=2017-08-10"
response = urllib.urlopen(url)
data = json.loads(response.read())['data']
logging.info("测试下载结果,%s",log_time)
with open("packet.sql","w") as F:
for i in data:
print("%s %s %s %s" %(type(str(i['time'])),type(str(i['patch_id'])),type(str(i['filename'])),type(str(i['server_ip']))))
F.write("%s %s %s %s\n" %(str(i['time']),str(i['patch_id']),str(i['filename']),str(i['server_ip'])))
但是当我试图运行该脚本,我得到一个TypeError
:
Traceback (most recent call last):
File "/usr/lib64/python2.6/logging/__init__.py", line 784, in emit
msg = self.format(record)
File "/usr/lib64/python2.6/logging/__init__.py", line 662, in format
return fmt.format(record)
File "/usr/lib64/python2.6/logging/__init__.py", line 444, in format
record.message = record.getMessage()
File "/usr/lib64/python2.6/logging/__init__.py", line 314, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
我已经它转换为格式字符串,为什么我收到:
TypeError: not all arguments converted during string formatting
当我删除F.write
F.write("%s %s %s %s\n" %(str(i['time']),str(i['patch_id']),str(i['filename']),str(i['server_ip'])))
这是正确的