小编Vin*_*jip的帖子

UnicodeEncodeError:'ascii'编解码器无法对位置0中的字符u'\ xef'进行编码:序数不在范围内(128)

我想解析我的XML文档.所以我存储了我的XML文档,如下所示

class XMLdocs(db.Expando):  
   id = db.IntegerProperty()    
   name=db.StringProperty()  
   content=db.BlobProperty()  
Run Code Online (Sandbox Code Playgroud)

现在我的下面是我的代码

parser = make_parser()     
curHandler = BasketBallHandler()  
parser.setContentHandler(curHandler)  
for q in XMLdocs.all():  
        parser.parse(StringIO.StringIO(q.content))
Run Code Online (Sandbox Code Playgroud)

我收到了以下错误

'ascii' codec can't encode character u'\xef' in position 0: ordinal not in range(128)
Traceback (most recent call last):  
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 517, in __call__
    handler.post(*groups)   
  File "/base/data/home/apps/parsepython/1.348669006354245654/mapreduce/base_handler.py", line 59, in post
    self.handle()   
  File "/base/data/home/apps/parsepython/1.348669006354245654/mapreduce/handlers.py", line 168, in handle
    scan_aborted = not self.process_entity(entity, ctx)   
  File "/base/data/home/apps/parsepython/1.348669006354245654/mapreduce/handlers.py", line 233, in process_entity
    handler(entity)   
  File "/base/data/home/apps/parsepython/1.348669006354245654/parseXML.py", line 71, in process
    parser.parse(StringIO.StringIO(q.content))   
  File …
Run Code Online (Sandbox Code Playgroud)

python google-app-engine xml-parsing

73
推荐指数
6
解决办法
20万
查看次数

UTF-8在Python日志中,如何?

我正在尝试使用Python的日志包将UTF-8编码的字符串记录到文件中.作为玩具示例:

import logging

def logging_test():
    handler = logging.FileHandler("/home/ted/logfile.txt", "w",
                                  encoding = "UTF-8")
    formatter = logging.Formatter("%(message)s")
    handler.setFormatter(formatter)
    root_logger = logging.getLogger()
    root_logger.addHandler(handler)
    root_logger.setLevel(logging.INFO)

    # This is an o with a hat on it.
    byte_string = '\xc3\xb4'
    unicode_string = unicode("\xc3\xb4", "utf-8")

    print "printed unicode object: %s" % unicode_string

    # Explode
    root_logger.info(unicode_string)

if __name__ == "__main__":
    logging_test()
Run Code Online (Sandbox Code Playgroud)

这会在logging.info()调用中与UnicodeDecodeError一起爆炸.

在较低级别,Python的日志包使用编解码器包打开日志文件,传递"UTF-8"参数作为编码.这一切都很好,但它试图将字节字符串写入文件而不是unicode对象,这会爆炸.从本质上讲,Python正在这样做:

file_handler.write(unicode_string.encode("UTF-8"))
Run Code Online (Sandbox Code Playgroud)

什么时候应该这样做:

file_handler.write(unicode_string)
Run Code Online (Sandbox Code Playgroud)

这是Python中的一个错误,还是我正在服用疯狂的药丸?FWIW,这是一个库存Python 2.6安装.

python unicode logging

44
推荐指数
4
解决办法
4万
查看次数

在PyQt应用程序中退出时提示

有没有办法让prom用户退出用Python编写的gui程序?

像"您确定要退出该计划吗?"

我正在使用PyQt.

python pyqt exit

19
推荐指数
1
解决办法
3万
查看次数

Python:如何抑制来自第三方库的日志记录语句?

我的日志设置看起来像

import requests
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('BBProposalGenerator')
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到日志

INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
INFO:BBProposalGenerator:created proposal: 763099d5-3c8a-47bc-8be8-b71a593c36ac
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
INFO:BBProposalGenerator:added offer with cubeValueId: f23f801f-7066-49a2-9f1b-1f8c64576a03
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
INFO:BBProposalGenerator:evaluated proposal: 763099d5-3c8a-47bc-8be8-b71a593c36ac
Run Code Online (Sandbox Code Playgroud)

如何从requests包中抑制日志语句?所以我只看到我的代码中的日志

INFO:BBProposalGenerator:created proposal: 763099d5-3c8a-47bc-8be8-b71a593c36ac
INFO:BBProposalGenerator:added offer with cubeValueId: f23f801f-7066-49a2-9f1b-1f8c64576a03
INFO:BBProposalGenerator:evaluated proposal: 763099d5-3c8a-47bc-8be8-b71a593c36ac
Run Code Online (Sandbox Code Playgroud)

谢谢

python logging

14
推荐指数
2
解决办法
4896
查看次数

如何配置我的log4j(使用Glassfish)登录日志目录,而不是配置?

我的log4j.properties文件中有以下行:

log4j.appender.logfile.File = MyApplication.log

我的日志文件出现在MyDomain/config目录中,但我希望它登陆MyDomain/logs目录.我怎样才能做到这一点?我不允许修改startserv脚本.

在此先感谢您的帮助!

java logging glassfish

9
推荐指数
3
解决办法
2万
查看次数

有没有办法强制before_filter始终执行最后一次?

我想在控制器中定义一个before_filter,但总是让它最后执行.

我知道append_before_filter,但是我想在一个模块中指定这个过滤器,其他类也可以在以后添加其他的before_filters.

有没有办法做到这一点?

ruby-on-rails

6
推荐指数
1
解决办法
2244
查看次数

Python - 关于登录 __init__.py 的问题

我有以下 python 包结构。

python_logging
    python_logging
        __init__.py
        first_class.py
        second_class.py
    run.py
Run Code Online (Sandbox Code Playgroud)

这里是代码 __init__.py

初始化.py

import logging
import logging.config


# Create the Logger
loggers = logging.getLogger(__name__)
loggers.setLevel(logging.DEBUG)

# Create the Handler for logging data to a file
logger_handler = logging.FileHandler(filename='C:\Python\Log\stest.txt')
logger_handler.setLevel(logging.DEBUG)

# Create a Formatter for formatting the log messages
logger_formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')

# Add the Formatter to the Handler
logger_handler.setFormatter(logger_formatter)

# Add the Handler to the Logger
loggers.addHandler(logger_handler)
loggers.info('Completed configuring logger()!') 
Run Code Online (Sandbox Code Playgroud)

这是 first_class.py 的代码

import logging

class …
Run Code Online (Sandbox Code Playgroud)

python logging

6
推荐指数
1
解决办法
6899
查看次数

为什么TinyMCE在(django){%}}标签之间插入<span> </ span>?

我的应用程序具有用于编辑Django模板的CMS.

每当我输入类似的东西

{% sometag %}
Run Code Online (Sandbox Code Playgroud)

TinyMCE实际上会存储

{%<span>sometag</span>%}
Run Code Online (Sandbox Code Playgroud)

有没有办法阻止TinyMCE这样做?

html django tinymce

5
推荐指数
1
解决办法
2306
查看次数

Django,查询集中的实际月份

怎么生病,根据我的查询集中的当前(实际)月份获取我的注册?,我有一个ModelManager(),它只显示LIVE寄存器状态,但现在我想显示具有LIVE状态和当前状态的寄存器(实际)月,我知道生病了像.filter(...),但我不知道如何得到当月...

model.py

#manager
class LiveNoticiaManager(models.Manager):
    def get_query_set(self):
        return super(LiveNoticiaManager,self).get_query_set().filter(status=self.model.LIVE_STATUS)
Run Code Online (Sandbox Code Playgroud)

多谢你们.

django django-models

5
推荐指数
1
解决办法
6933
查看次数

尽管已正确设置,但Python记录器不会打印调试消息

我有以下代码,我只想 使用contextmanager来使用日志记录模块.

from contextlib import contextmanager
import logging

@contextmanager
def log_level(level, name):
    logger = logging.getLogger(name)
    old_level = logger.getEffectiveLevel()
    print('log_level.old_level: ' + str(old_level))
    logger.setLevel(level)
    print('log_level.new_level: ' + str(logger.getEffectiveLevel()))
    try:
        yield logger
    finally:
        logger.setLevel(old_level)

if __name__ == '__main__':

    with log_level(logging.DEBUG, 'my-log') as logger:
        print('__main__.log_level.logger.level: ' + str(logger.getEffectiveLevel()))
        logger.debug('Debug with logger: will print')
        logger.warning('Warning')
        print('__main__.log_level.logger.level: ' + str(logger.getEffectiveLevel()))
    print('__main__.logger.level: ' + str(logger.getEffectiveLevel()))
Run Code Online (Sandbox Code Playgroud)

可以看出,在main.log_level中,记录器级别应该是DEBUG,它应该打印消息'Debug with logger:will print'.但是,当我运行代码时,此调试消息不会打印.查看代码的打印件,它表示记录器在log_level内部具有DEBUG级别,并且当它退出log_level时级别返回到WARNING.这是我的输出,当使用python 3执行时:

log_level.old_level: 30
log_level.new_level: 10
__main__.log_level.logger.level: 10
Warning
__main__.log_level.logger.level: 10
__main__.logger.level: 30
Run Code Online (Sandbox Code Playgroud)

我想帮助理解为什么 …

python logging contextmanager python-3.x

5
推荐指数
1
解决办法
5486
查看次数