我有一个代码,这样:
a = "\u0432"
b = u"\u0432"
c = b"\u0432"
d = c.decode('utf8')
print(type(a), a)
print(type(b), b)
print(type(c), c)
print(type(d), d)
Run Code Online (Sandbox Code Playgroud)
并输出:
<class 'str'> ?
<class 'str'> ?
<class 'bytes'> b'\\u0432'
<class 'str'> \u0432
Run Code Online (Sandbox Code Playgroud)
为什么在后一种情况下我看到的是字符代码,而不是字符?我如何将Byte字符串转换为Unicode字符串,在输出的情况下,我看到了字符而不是代码?
我只是python中的新手,对于noobish问题感到抱歉
>>> import os
>>> os.listdir("/home/user/Desktop/1")
['1.txt', '2', '3.txt']
>>> os.path.isfile("/home/user/Desktop/1/1.txt")
True
>>> for i in os.listdir("/home/user/Desktop/1"):
... print(os.path.isfile(i))
...
False
False
False
>>>
Run Code Online (Sandbox Code Playgroud)
其中两个是文件,那么当它应该是True时输出为False的原因是什么?
我们在项目中使用Python 3.x. 但Protocol Buffers的官方客户端只支持python 2.x.
我不想降级到python 2.x.
安装python 3.1后,我无法打印任何东西.这是一个例子:
>>> print "Hello World"
File "<stdin>", line 1
print "Hello World"
^
SyntaxError: invalid syntax
>>>
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个错误?
我有jQuery代码,当我点击它首先隐藏的链接然后删除一些HTML,如下所示:
$(this).parent().parent().hide('slow', function () {
$(this).remove();
});
Run Code Online (Sandbox Code Playgroud)
我想进行一个QUnit测试,确保删除有问题的HTML:
$(thelink).click();
// Check that it is gone, by finding the first item in the list
entity = input.form.find('.recurrenceinput_occurrences .occurrence span.action a')[0];
// And make sure it's NOT the deleted one:
ok(entity.attributes.date.value !== "20110413T000000");
Run Code Online (Sandbox Code Playgroud)
问题当然是在隐藏动画运行结束之前运行ok()测试,因此尚未删除有问题的HTML,并且测试失败.
我已经尝试了各种方法来延迟/停止测试一秒左右,但似乎没有任何工作.最明显的一个是使用asynTest而且可以
stop();
setTimeout(start, 2000);
Run Code Online (Sandbox Code Playgroud)
但这实际上并没有阻止测试.它确实似乎停了两秒钟,但我不确定是什么.:-)
有任何想法吗?
我有点惊讶的是,使用Python获取网页的charset非常复杂.我错过了一条路吗?HTTPMessage有很多函数,但不是这个.
>>> google = urllib2.urlopen('http://www.google.com/')
>>> google.headers.gettype()
'text/html'
>>> google.headers.getencoding()
'7bit'
>>> google.headers.getcharset()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: HTTPMessage instance has no attribute 'getcharset'
Run Code Online (Sandbox Code Playgroud)
所以你必须得到标题,并拆分它.两次.
>>> google = urllib2.urlopen('http://www.google.com/')
>>> charset = 'ISO-8859-1'
>>> contenttype = google.headers.getheader('Content-Type', '')
>>> if ';' in contenttype:
... charset = contenttype.split(';')[1].split('=')[1]
>>> charset
'ISO-8859-1'
Run Code Online (Sandbox Code Playgroud)
对于这样一个基本功能来说,这是一个惊人的步骤.我错过了什么吗?
我有"词干"和"结尾"(可能不是正确的单词)的映射,如下所示:
all_endings = {
'birth': set(['place', 'day', 'mark']),
'snow': set(['plow', 'storm', 'flake', 'man']),
'shoe': set(['lace', 'string', 'maker']),
'lock': set(['down', 'up', 'smith']),
'crack': set(['down', 'up',]),
'arm': set(['chair']),
'high': set(['chair']),
'over': set(['charge']),
'under': set(['charge']),
}
Run Code Online (Sandbox Code Playgroud)
但当然要长得多.我也用相反的方式制作了相应的字典:
all_stems = {
'chair': set(['high', 'arm']),
'charge': set(['over', 'under']),
'up': set(['lock', 'crack', 'vote']),
'down': set(['lock', 'crack', 'fall']),
'smith': set(['lock']),
'place': set(['birth']),
'day': set(['birth']),
'mark': set(['birth']),
'plow': set(['snow']),
'storm': set(['snow']),
'flake': set(['snow']),
'man': set(['snow']),
'lace': set(['shoe']),
'string': set(['shoe']),
'maker': set(['shoe']),
}
Run Code Online (Sandbox Code Playgroud)
我现在试图想出一个算法来找到两个或多个匹配两个或多个"结尾"的"茎"的匹配.例如,在上面,它将与锁定和裂缝向下和向上匹配,从而产生
lockdown
lockup
crackdown
crackup
Run Code Online (Sandbox Code Playgroud)
但不包括 …
我正在尝试使用csv模块读取utf-8 csv文件,由于编码,我在创建python 2和3的通用代码时遇到了一些麻烦.
这是Python 2.7中的原始代码:
with open(filename, 'rb') as csvfile:
csv_reader = csv.reader(csvfile, quotechar='\"')
langs = next(csv_reader)[1:]
for row in csv_reader:
pass
Run Code Online (Sandbox Code Playgroud)
但是当我用python 3运行它时,它不喜欢我没有"编码"打开文件的事实.我试过这个:
with codecs.open(filename, 'r', encoding='utf-8') as csvfile:
csv_reader = csv.reader(csvfile, quotechar='\"')
langs = next(csv_reader)[1:]
for row in csv_reader:
pass
Run Code Online (Sandbox Code Playgroud)
现在python 2无法解码"for"循环中的行.那么......我该怎么做?
我尝试使用Wing IDE(v.4.1.3)和Komodo IDE(v.7.0.0)调试Python 3.因为,预计调试器会增加大量的运行时开销.但令我惊讶的是调试器之间的差异.
以下是同一程序的运行时间.没有断点或其他任何东西,只是没有任何实际调试的常规运行:
我将调试器称为匿名#1和#2,以免这成为其中一个的无意(并可能被误导)广告.
其中一个调试器真的是8倍"更快"吗?
或者是否有一些设计权衡,更快的调试器放弃一些功能,或精度,或鲁棒性,或其他什么,以换取更快的速度?如果是这样,我想知道这些细节,无论是专门用于Wing/Komodo还是一般的Python调试器.
我正在尝试在Python中实现SVG路径计算,但我遇到了Arc曲线的问题.
我认为问题在于从终点到中心参数化的转换,但我找不到问题.您可以在SVG规范的 F6.5节中找到有关如何实现它的说明.我也看过其他语言的实现,我也看不出它们有什么不同.
我的Arc对象实现在这里:
class Arc(object):
def __init__(self, start, radius, rotation, arc, sweep, end):
"""radius is complex, rotation is in degrees,
large and sweep are 1 or 0 (True/False also work)"""
self.start = start
self.radius = radius
self.rotation = rotation
self.arc = bool(arc)
self.sweep = bool(sweep)
self.end = end
self._parameterize()
def _parameterize(self):
# Conversion from endpoint to center parameterization
# http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
cosr = cos(radians(self.rotation))
sinr = sin(radians(self.rotation))
dx = (self.start.real - self.end.real) / 2
dy = (self.start.imag …Run Code Online (Sandbox Code Playgroud) python ×8
python-3.x ×5
content-type ×1
csv ×1
debugging ×1
encoding ×1
http ×1
javascript ×1
jquery ×1
math ×1
performance ×1
python-2.7 ×1
qunit ×1
string ×1
svg ×1
unicode ×1