我可以在 sublime text 2(并使用 python3.2 构建)中为 build 命令设置 python3.2,但是在调用控制台时cmd-`解释器是 mac 的默认 2.6 版本。任何帮助是极大的赞赏!
python3 datetime.datetime.strftime无法接受utf-8字符串格式
我做的是::
# encoding: utf-8
import datetime
f = "%Y?%m?%d?"
now = datetime.datetime.now()
print( now.strftime(f) )
Run Code Online (Sandbox Code Playgroud)
而我得到的是:
D:\pytools>python a.py
Traceback (most recent call last):
File "a.py", line 6, in <module>
print( now.strftime(f) )
UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2
: Illegal byte sequence
Run Code Online (Sandbox Code Playgroud)
为什么以及如何解决这个问题?
匹配此正则表达式的过程是什么?我不明白为什么显式组是'c'.这段代码取自Python Re Module Doc.
>>> m = re.match("([abc])+", "abc")
>>> m.group()
'abc'
>>> m.groups()
('c',)
Run Code Online (Sandbox Code Playgroud)
那么,怎么样:
>>> m = re.match("([abc]+)", "abc")
>>> m.group()
'abc'
>>> m.groups()
('abc',)
Run Code Online (Sandbox Code Playgroud)
和:
>>> m = re.match("([abc])", "abc")
>>> m.group()
'a'
>>> m.groups()
('a',)
Run Code Online (Sandbox Code Playgroud)
谢谢.
在我的python代码中有:
print "Hello"
time.sleep(20)
print "world"
Run Code Online (Sandbox Code Playgroud)
我期望输出为
Hello
Run Code Online (Sandbox Code Playgroud)
然后20秒后
world
Run Code Online (Sandbox Code Playgroud)
但是Hello和world正在控制台中同时打印。
我有一个函数接受字典作为参数(从另一个有效的函数返回).这个函数应该要求输入一个字符串,并查看字典中的每个元素,看看它是否在那里.字典基本上是三字母缩写:国家即:AFG:阿富汗等等.如果我将'sta'作为我的字符串,它应该将任何具有该团队STATY,AfghaniSTAn,coSTA rica等片段的国家附加到初始化的空列表中,然后返回所述列表.否则,它返回[未找到].返回列表应如下所示:[['Code','Country'],['USA','United States'],['CRI','Costa Rica'],['AFG','Afganistan']]这是我的代码到目前为止的样子:
def findCode(countries):
some_strng = input("Give me a country to search for using a three letter acronym: ")
reference =['Code','Country']
code_country= [reference]
for key in countries:
if some_strng in countries:
code_country.append([key,countries[key]])
if not(some_strng in countries):
code_country.append( ['NOT FOUND'])
print (code_country)
return code_country
Run Code Online (Sandbox Code Playgroud)
我的代码一直在返回['找不到']
如何将 CookieJar 加载到新的 requests.Session 对象?
cj = cookielib.MozillaCookieJar("mycookies.txt")
s = requests.Session()
Run Code Online (Sandbox Code Playgroud)
这就是我创建的,现在会话将存储cookie,但我希望它从文件中获取cookie
(会话应该加载cookieJar)。如何才能实现这一目标?
我搜索了文档,但只能找到代码示例,而且它们从不加载 cookieJar,只是在一个会话期间保存 cookie。
我有一个清单 [[4,5,6],[2,3,1]].现在我想根据list[1]输出 排序列表[[6,4,5],[1,2,3]].所以基本上我正在排序2,3,1和维护顺序list[0].
在搜索时,我得到了一个基于每个列表的第一个元素进行排序的函数,但不是为此.此外,我不想重新创建列表[[4,2],[5,3],[6,1]],然后使用该功能.
这次我正在尝试Solem博客的另一个例子.它是一个通过使用霍夫变换检测图像中的线条和圆圈的模块.这是代码(houghlines.py):
import numpy as np
import cv2
"""
Script using OpenCV's Hough transforms for reading images of
simple dials.
"""
# load grayscale image
im = cv2.imread("house2.jpg")
gray_im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
# create version to draw on and blurred version
draw_im = cv2.cvtColor(gray_im, cv2.COLOR_GRAY2BGR)
blur = cv2.GaussianBlur(gray_im, (0,0), 5)
m,n = gray_im.shape
# Hough transform for circles
circles = cv2.HoughCircles(gray_im, cv2.cv.CV_HOUGH_GRADIENT, 2, 10, np.array([]), 20, 60, m/10)[0]
# Hough transform for lines (regular and probabilistic)
edges = cv2.Canny(blur, …Run Code Online (Sandbox Code Playgroud) 我们遇到了一个小问题,这让我们很烦恼.让我快速解释一下我们在做什么:
我们正在创建一个Windows窗体,将其另存为.DLL并加载一个MDIContainer.看起来很好,工作正常,但是,如果我们在Form中使用Panel作为组件,它会改变大小.
之前:

之后(在MDIContainer中):

(注意面板!).
我们猜测这是因为我们的自定义MDI容器.这是我们的MDI容器的代码:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace NAMESPACE.Forms
{
class MdiClientPanel : Panel
{
private Form mdiForm;
private MdiClient ctlClient = new MdiClient();
public MdiClientPanel()
{
this.ctlClient.BackColor = Color.LightGray;
base.Controls.Add(this.ctlClient);
}
public Form MdiForm
{
get
{
if (this.mdiForm == null)
{
this.mdiForm = new Form();
System.Reflection.FieldInfo field = typeof(Form).GetField("ctlClient", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
field.SetValue(this.mdiForm, this.ctlClient);
}
return this.mdiForm;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以解决这个问题吗?谢谢你的帮助.
//编辑:添加了赏金,因为我们想知道为什么会这样.你如何重现它:
我确信这很容易:
说我有以下字典:
foo = { a: 3, b: 10, c: 5 }
Run Code Online (Sandbox Code Playgroud)
获得价值10的最有效(也是最干净)的方法是什么?
谢谢
python ×9
python-2.7 ×2
python-3.x ×2
c# ×1
cookiejar ×1
dictionary ×1
httprequest ×1
list ×1
nested ×1
nonetype ×1
regex ×1
sorting ×1
sublimetext2 ×1
typeerror ×1
unicode ×1