【发布时间】:2015-02-20 16:37:34
【问题描述】:
Python 2.x CSV 输入示例(我有很多)
67.60.60.24, 1384, application/octet-stream,18/Feb/2015:00:00:50
期望的输出:
insert into mytable (ip, bytes, type, timestamp) values ('67.60.60.24', 1384, 'application/octet-stream', '2015-02-18')
到目前为止:
sqlTemplate = "insert into mytable (ip, bytes, type, timestamp) values ('{0}', {1}, '{2}', '{3}')
# lines contains many of the above sample input CSV lines
for line in lines:
list = line.split(",")
list = [elem.strip() for elem in list] # whitespace strip each element
# Make sure len(list) == 4
if len(list) != 4:
continue
sql = sqlTemplate.format(list) # how can i apply the list to the args?
# todo: insert sql into DB
【问题讨论】:
-
不要使用字符串格式生成 SQL。请改用 SQL 参数。
-
哦...是的!谢谢!
标签: python list string-formatting