python - 数据库 及 TypeError: 'zip' object is not subscriptable
2018-01-24 17:49
766 查看
import numpy as np import pandas as pd from pandas import Series, DataFrame # pandas 数据分析包 #使用数据库 import sqlite3 query = """ CREATE TABLE test (a VARCHAR(20), b VARCHAR(20), c REAL, d INTEGER );""" con = sqlite3.connect(':memory:') con.execute(query) con.commit() data = [('Atlanta', 'Georgia', 1.25, 6), ('Tallahassee', 'Florida', 2.6, 3), ('Sacramento', 'California', 1.7, 5)] stmt = "INSERT INTO test VALUES(?, ?, ?, ?)" con.executemany(stmt, data) con.commit() cursor = con.execute('select * from test') rows = cursor.fetchall() print( rows ) ''' [('Atlanta', 'Georgia', 1.25, 6), ('Tallahassee', 'Florida', 2.6, 3), ('Sacramento', 'California', 1.7, 5)] ''' cursor.description DataFrame(rows, columns=list(zip(*cursor.description))[0]) ''' DataFrame(rows, columns=zip(*cursor.description)[0]) 运行报错:TypeError: 'zip' object is not subscriptable 因为 python 3.x 需要 list()后,再索引查找元素 改为 DataFrame(rows, columns=list(zip(*cursor.description))[0]) ''' import pandas.io.sql as sql _aa = sql.read_sql('select * from test', con) print( _aa ) ''' a b c d 0 Atlanta Georgia 1.25 6 1 Tallahassee Florida 2.60 3 2 Sacramento California 1.70 5 '''
相关文章推荐
- TypeError: object of type 'zip' has no len()、'zip' object is not subscriptable
- Python的坑(2) -- TypeError: 'builtin_function_or_method' object is not subscriptable
- python 2.7 : 引用模块时出现TypeError:'module' object is not callable
- python TypeError: 'builtin_function_or_method' object is not iterable keys
- python3.5 模拟UDP客户端发送数据报文,报错:TypeError: a bytes-like object is required, not 'str'
- python 3.5: TypeError: a bytes-like object is required, not 'str' when writing to a file
- 【Python 笔记】命名域之 TypeError 'DataFrame' object is not callable
- Python 3.x: TypeError: a bytes-like object is required, not 'str'
- python socket send 函数 报错:TypeError: a bytes-like object is required, not 'str'
- Python中3+版本运行2+版本程序出现TypeError: a bytes-like object is required, not 'str'之读取PDF中的图片
- Python包导入(TypeError: 'module' object is not callable),及一定要记住Python成员函数第一个参数值是self。
- Python错误代码分析:莫名的[TypeError: 'int' object is not iterable]
- Python:TypeError: 'int' object is not callable
- python TypeError: 'int' object is not iterable
- “TypeError: 'dict_keys' object is not subscriptable”
- python错误:TypeError: 'str' object is not callable
- Python出现TypeError: 'NoneType' object is not iterable
- Python: TypeError: 'dict' object is not callable
- python+selenium自动化测试~打开浏览器报TypeError: 'module' object is not callable
- python TypeError: 'module' object is not callable