小编Nic*_*son的帖子

加入时的UnicodeDecodeError

我有一个包含一些字符串的列表(其中大部分是从sqlite3数据库中获取的):

stats_list = ['Statistik \xc3\xb6ver s\xc3\xa5nger\n', 'Antal\tS\xc3\xa5ng', '1\tCarola - Betlehems Stj\xc3\xa4rna', '\n\nStatistik \xc3\xb6ver datak\xc3\xa4llor\n', 'K\xc3\xa4lla\tAntal', 'MANUAL\t1', '\n\nStatistik \xc3\xb6ver \xc3\xb6nskare\n', 'Antal\tId', u'1\tNiclas']
Run Code Online (Sandbox Code Playgroud)

当我尝试加入时:

return '\n'.join(stats_list)
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 10: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

是否有可能通过查看列表得到任何线索?如果我遍历列表并将其打印到屏幕,我得到这个:

Statistik över sånger

Antal   Sång 
1   Carola - Betlehems Stjärna


Statistik över datakällor

Källa   Antal 
MANUAL  1


Statistik över önskare

Antal   Id
1   Niclas

这正是我所期待的,并没有显示错误.(特殊字符是瑞典语).

编辑:

我试过这个:

   return '\n'.join(i.decode('utf8') for i in stats_list)
Run Code Online (Sandbox Code Playgroud)

但它回来了:

Traceback (most recent call last):
  File …
Run Code Online (Sandbox Code Playgroud)

python unicode character-encoding

13
推荐指数
2
解决办法
9963
查看次数

不要从命令行中读取unicode吗?

运行Python 2.7

执行时:

$ python client.py get_emails -a "åäö"
Run Code Online (Sandbox Code Playgroud)

我明白了:

usage: client.py get_emails [-h] [-a AREA] [-t {rfc2822,plain}]
client.py get_emails: error: argument -a/--area: invalid unicode value: '\xc3\xa5\xc3\xa4\xc3\xb6'
Run Code Online (Sandbox Code Playgroud)

这是我的解析器:

def _argparse():
    desc = """
           Simple CLI-client for...
           """
    argparser = argparse.ArgumentParser(description=desc)
    subparsers = argparser.add_subparsers(dest='command')

    # create the parser for the "get_emails" command
    parser_get_emails = subparsers.add_parser('get_emails', help=u'Get email list')
    parser_get_emails.add_argument('-a', '--area', type=unicode, help='Limit to area')
    parser_get_emails.add_argument('-t', '--out_type', choices=['rfc2822', 'plain'],
                                   default='rfc2822', help='Type of output')

    args = argparser.parse_args()
    return args
Run Code Online (Sandbox Code Playgroud)

这是否意味着我不能在python argparse模块中使用任何unicode字符?

python unicode argparse

12
推荐指数
2
解决办法
6633
查看次数

定时功能

警告,这有点递归;)

我回答了这个问题:Python:如何在最长元素之前获取列表中的所有元素?

在我提交之后,另一个应该更快的答案(作者认为,我也是如此).我试图计算不同的解决方案,但应该更慢的解决方案实际上更快.这让我觉得我的代码有问题.或者是吗?

import string
import random
import time

def solution1(lst):
  return lst[:lst.index(max(lst, key=len))]

def solution2(lst):
  idx, maxLenStr = max(enumerate(lst), key=lambda x:len(x[1]))
  return lst[:idx]

# Create a 100000 elements long list that contains
# random data and random element length
lst = []
for i in range(100000):
  s = "".join([random.choice(string.letters+string.digits) for x in range(1, random.randint(1,50))])
  lst.append(s)

# Time the first solution
start = time.time()
solution1(lst)
print 'Time for solution1', (time.time() - start)

# Time the second solution
start = …
Run Code Online (Sandbox Code Playgroud)

python time

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

如何在wxglade中添加自定义小部件

我有一个用 wxglade 制作的应用程序。我添加了一个媒体控件来播放 mp3:s。如果没有媒体控制,应用程序将以 800x600px 的框架启动。但是当我添加媒体控件时,框架非常小。我相信这是因为我没有将其添加到 sizer 中。但我必须这样做吗?无论如何,媒体控件不应该显示任何内容。

所以,我的问题是,如何将媒体控件添加到我的应用程序中,而不会(a)破坏使用 wxglade 更新 gui 的可能性以及(b)失去以正确大小启动的能力?

如果可以的话我不会在# Begin wxglade和之间放置任何东西# End wxglade。因为如果我用 wxglade 更改我的 gui(无论如何根据早期的测试),它就会被破坏。

编辑:我提供的代码片段没有添加任何有趣的内容。我还编辑了问题以使其更加清晰,以便其他有相同问题的人可以找到答案。

python wxpython wxglade

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

Joomla管理员重定向到安装

我有一个Joomla 2.5站点,我无法再访问管理员面板.这只会将我重定向到/installation/index.php(它应该)给出404.我认为这是在升级之后发生的.

我试图清除浏览器缓存,如下所示:Joomla 1.0,管理员链接重定向到"installation/index.php",如何防止这种重定向?

但那没用.

很久以前,在1.x天之后,在Joomla网站上有一些安全建议,其中包括建议将configuration.php移到我遵循的public_html目录之外.我现在找不到它们,但我尝试将configuration.php文件复制到Joomla根目录,并将其保存在define.php(define('JPATH_CONFIGURATION')中定义的目录中.

有任何想法吗?

我的扩展

bash-3.2$ find plugins/ -type d
plugins/
plugins/authentication
plugins/authentication/gmail
plugins/authentication/joomla
plugins/authentication/ldap
plugins/captcha
plugins/captcha/recaptcha
plugins/content
plugins/content/emailcloak
plugins/content/finder
plugins/content/geshi
plugins/content/geshi/geshi
plugins/content/geshi/geshi/geshi
plugins/content/joomla
plugins/content/loadmodule
plugins/content/pagebreak
plugins/content/pagenavigation
plugins/content/vote
plugins/editors-xtd
plugins/editors-xtd/article
plugins/editors-xtd/image
plugins/editors-xtd/pagebreak
plugins/editors-xtd/readmore
plugins/editors
plugins/editors/codemirror
plugins/editors/none
plugins/editors/tinymce
plugins/extension
plugins/extension/joomla
plugins/finder
plugins/finder/categories
plugins/finder/contacts
plugins/finder/content
plugins/finder/newsfeeds
plugins/finder/weblinks
plugins/quickicon
plugins/quickicon/extensionupdate
plugins/quickicon/joomlaupdate
plugins/search
plugins/search/categories
plugins/search/contacts
plugins/search/content
plugins/search/newsfeeds
plugins/search/weblinks
plugins/system
plugins/system/cache
plugins/system/debug
plugins/system/highlight
plugins/system/languagecode
plugins/system/languagecode/language
plugins/system/languagecode/language/en-GB
plugins/system/languagefilter
plugins/system/log
plugins/system/logout
plugins/system/p3p
plugins/system/redirect
plugins/system/remember
plugins/system/sef
plugins/user
plugins/user/contactcreator
plugins/user/joomla
plugins/user/profile
plugins/user/profile/fields
plugins/user/profile/profiles
Run Code Online (Sandbox Code Playgroud)

php .htaccess joomla redirect

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

django-celery-results将不会收到结果

我安装了芹菜,并与django一起工作。我有一些定期运行的任务。芹菜日志显示任务已执行,并且返回了某些内容。

[2017-03-26 14:34:27,039: INFO/MainProcess] Received task: my_webapp.apps.events.tasks.clean_outdated[87994396-04f7-452b-a964-f6bdd07785e0]  
[2017-03-26 14:34:28,328: INFO/PoolWorker-1] Task my_webapp.apps.events.tasks.clean_outdated[87994396-04f7-452b-a964-f6bdd07785e0] succeeded in 0.05246314400005758s: 'Removed 56 event(s)

 | Removed 4 SGW(s)

'
Run Code Online (Sandbox Code Playgroud)

但是结果未在django-celery-results管理页面上显示。

这些是我的设置:

CELERY_BROKER_URL = os.environ.get('BROKER_URL')
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Europe/Stockholm'
CELERY_RESULT_BACKEND = 'django-cache'
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'
CELERY_RESULT_DB_SHORT_LIVED_SESSIONS = True  # Fix for low traffic sites like this one
Run Code Online (Sandbox Code Playgroud)

我也尝试设置CELERY_RESULT_BACKEND = 'django-db'。我知道进行迁移(使用这些设置时),该表存在于数据库中:

my_webapp=> \dt
                           List of relations
 Schema |                 Name                 | Type  |     Owner      
--------+--------------------------------------+-------+----------------
 ...
 public | django_celery_beat_crontabschedule …
Run Code Online (Sandbox Code Playgroud)

celery django-celery

5
推荐指数
0
解决办法
460
查看次数

如何在 wxgrid (python) 中放置组合框?

我找不到任何关于如何将组合框放置在 wxgrid 单元格内的好信息。我试过谷歌,但也许我是瞎子。有人有一个简单的示例可以分享吗?

python wxpython

4
推荐指数
1
解决办法
4062
查看次数

使用 configparser 添加注释

我可以使用 python 中的 ConfigParser 模块使用 add_section 和 set 方法创建 ini 文件(参见http://docs.python.org/library/configparser.html 中的示例)。但我没有看到任何关于添加评论的内容。那可能吗?我知道使用 # 和 ; 但是如何让 ConfigParser 对象为我添加它?我在 configparser 的文档中没有看到任何关于此的内容。

python configparser

4
推荐指数
1
解决办法
4062
查看次数

用于diff sqlite表的Git钩子

我在Git存储库中有一个Sqlite数据库.今天我想在两个不同的提交中做一个视图的差异.我是这样做的:

$ sqlite3 -list file.sqlite "SELECT * FROM contact_list_detailed" >/tmp/newlist
$ git checkout 51c24d13c file.sqlite
$ sqlite3 -list file.sqlite "SELECT * FROM contact_list_detailed" >/tmp/oldlist
$ git checkout -- file.sqlite
$ diff /tmp/oldlist /tmp/newlist
Run Code Online (Sandbox Code Playgroud)

它工作,如果我想,我可以编写脚本.但是有没有任何"好"的方法来做钩子这样做?

git sqlite githooks

3
推荐指数
3
解决办法
1472
查看次数

正确关闭插座

我正在尝试使用套接字建立服务器/客户端连接.但他们不能正常关闭,我无法理解为什么.

更新1

我已经纠正了我的愚蠢错误,而不是实际上在问题中调用s.close函数.但结果证明这不是我的问题.

更新结束

这是我的服务器代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket
import sys

if __name__ == '__main__':
    # Server connection
    s = socket.socket()          # Create a socket object
    host = socket.gethostname()  # Get local machine name
    port = 12345                 # Reserve a port for your service.

    print 'Server started!'
    print 'Waiting for clients...'

    s.bind((host, port))        # Bind to the port
    s.listen(5)                 # Now wait for client connection.
    c, addr = s.accept()     # Establish connection with client.
    print …
Run Code Online (Sandbox Code Playgroud)

python sockets

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