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'])))
這是正確的