python出错:AttributeError: 'int' object has no attribute 'encode'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python出错:AttributeError: 'int' object has no attribute 'encode'相关的知识,希望对你有一定的参考价值。

部分代码:

conn=pymysql.Connect(
host = 'localhost',
port = 3306,
user = 'root',
password = 'na123123',
db = 't1',
charset = 'utf8'
)

with conn:
cur = conn.cursor()
cur.execute("SELECT * FROM t1.remark WHERE idnum < '%d'" % 6000000)
rows = cur.fetchall()
comment = []

for row in rows:
row = list(row)
comment.append(row[0])
def train_model(texts):
for li in texts:
comm = li.decode('utf-8')

运行到最后一行时出错,数据库中的数据并没有编码,希望提供修改后的代码
comm在之后一直要用到

int不能做encode啊,你做encode前要保证你的encode对象是str啊

比如:

x=16
x.encode('utf-8')就会发生你这种错误
你要先
x=str(x)
然后
x.encode('utf-8')

就行了追问

是不是不需要解码?

我改成这样没报错 但是也没有运行结果

参考技术A

运行到rows = cur.fetchall()就报错了呀

将库运行到 python 时出错

【中文标题】将库运行到 python 时出错【英文标题】:Error to run a library to python 【发布时间】:2017-02-05 08:53:06 【问题描述】:

我按照http://npatta01.github.io/2015/08/10/dlib/的步骤操作,但是当我尝试运行时(我使用sudo),

python python_examples/face_detector.py examples/faces/2007_007763.jpg

收回错误。 首先,错误是

AttributeError: 'module' object has no attribute 'image_window' 

到第 8 行。 现在,错误是Illegal instruction (core dumped),但我不知道为什么。 请帮我正确添加库。

import sys

import dlib
from skimage import io


detector = dlib.get_frontal_face_detector()
win = dlib.image_window()

for f in sys.argv[1:]:
    print("Processing file: ".format(f))
    img = io.imread(f)
    # The 1 in the second argument indicates that we should upsample the image
    # 1 time.  This will make everything bigger and allow us to detect more
    # faces.
    dets = detector(img, 1)
    print("Number of faces detected: ".format(len(dets)))
    for i, d in enumerate(dets):
        print("Detection : Left:  Top:  Right:  Bottom: ".format(
            i, d.left(), d.top(), d.right(), d.bottom()))

    win.clear_overlay()
    win.set_image(img)
    win.add_overlay(dets)
    dlib.hit_enter_to_continue()


# Finally, if you really want to you can ask the detector to tell you the score
# for each detection.  The score is bigger for more confident detections.
# The third argument to run is an optional adjustment to the detection threshold,
# where a negative value will return more detections and a positive value fewer.
# Also, the idx tells you which of the face sub-detectors matched.  This can be
# used to broadly identify faces in different orientations.
if (len(sys.argv[1:]) > 0):
    img = io.imread(sys.argv[1])
    dets, scores, idx = detector.run(img, 1, -1)
    for i, d in enumerate(dets):
        print("Detection , score: , face_type:".format(
            d, scores[i], idx[i]))

【问题讨论】:

你能发布你的代码吗?发生的情况是您试图访问另一个模块中名为 image_window 的函数或对象,但它不存在。 欢迎来到 ***。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布代码并准确描述问题之前,我们无法有效地帮助您。 由于您没有充分描述您开始时所做的事情,您所做的更改,也没有提供完整的错误消息,我们真的无法提供帮助。 @PatrickHaugh 是的,关于 'win = dlib.image_window()' 的错误,但为什么?在其他电脑上运行。 @Prune 我不知道我改变了什么。在第一个错误之后,我尝试使用 eclipse 将库运行到 C/C++,但我也失败了,我再次尝试将库运行到 python,并且出现了第二个错误。 【参考方案1】:

正如我在您的代码中看到的:

detector = dlib.get_frontal_face_detector()
win = dlib.image_window()

第一行有效,第二行无效。这意味着 dlib 已安装,但它是在不支持 GUI 的情况下编译的

In dlib's source code 我们看到如果定义了宏 DLIB_NO_GUI_SUPPORT - dlib 模块中将没有“image_window”函数。如果 CMake 脚本找不到 X11 库,则会自动定义此宏

您需要确保编译 dlib 时支持 GUI。要做到这一点,首先 - 如果您在 Linux 上工作,请将 libx11-dev 安装到您的系统中,或者在 Mac 上安装 XQuartz

在运行 python setup.py install --yes DLIB_JPEG_SUPPORT 构建 dlib 时 - 检查其消息。如果有错误或警告 - 修复它们

【讨论】:

你是对的。来自python setup.py install --yes DLIB_JPEG_SUPPORT 的消息是CMake Error at /home/ekotsoni/dlib/dlib/cmake_utils/add_python_module:115 (message): Boost python library not found. Call Stack (most recent call first): CMakeLists.txt:6 (include) -- Configuring incomplete, errors occurred! See also "/home/ekotsoni/dlib/tools/python/build/CMakeFiles/CMakeOutput.log". error: cmake configuration failed! 我该怎么办? sudo apt-get install libboost-python-dev【参考方案2】:

我回答这个问题是因为我遇到了同样的问题

conda install -c conda-forge dlib

pip install dlib

我尝试搜索并获得了几个有用的链接,下面的一个链接为我节省了一天。所以在这里也列出详细信息..

https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf

从 Github 编译最新的代码比从 conda / pip 安装要好。这是为了确保 dlib 编译时支持 GUI。

安装依赖

sudo apt-get update

安装 Boost

sudo apt-get install libboost-all-dev

安装其他依赖项(可能大部分已经安装在您的系统中)

apt-get install -y --fix-missing     build-essential     cmake     gfortran     git     wget     curl     graphicsmagick     libgraphicsmagick1-dev     libatlas-dev     libavcodec-dev     libavformat-dev     libboost-all-dev     libgtk2.0-dev     libjpeg-dev     liblapack-dev     libswscale-dev     pkg-config     python3-dev     python3-numpy     software-properties-common zip

apt-get clean

从 Github 构建最新的 dlib 代码。 假设: - Ubuntu 16.04 或更高版本 - 没有 nVidia GPU,没有安装 Cuda 和 cuDNN,也不想使用 GPU 加速

从github克隆代码:

git clone https://github.com/davisking/dlib.git

构建主 dlib 库:

cd dlib
    mkdir build; cd build; cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1; cmake --build .

构建和安装 Python 扩展:

cd ..
    python setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA

确保指向正确的 python(如果你在 Ubuntu 的 vanilla python 之上安装了 anaconda,那么你应该安装指向 anaconda 的包)。

如果您仍然面临如下 gcc 错误

lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found

然后确保您正在安装以下 python 包

conda install libgcc

此时,你应该可以运行python并成功输入import dlib了。

【讨论】:

以上是关于python出错:AttributeError: 'int' object has no attribute 'encode'的主要内容,如果未能解决你的问题,请参考以下文章

加载spacy模型时出错AttributeError:module'msgpack._unpacker'没有属性'unpack'

将库运行到 python 时出错

python连接sqlite3出错

查找“fibo.py”规范时出错(<class 'AttributeError'>: 'module' object has no attribute '__path__')

AttributeError: ‘dict_keys‘ object has no attribute ‘sort‘

AttributeError: ‘dict_keys‘ object has no attribute ‘sort‘