我试图让python给我百分比编码的字符串.我正在与之交互的API(我认为使用百分比编码的UTF-8)给出了%c3%ae.但是,python的urllib.quote给出%3F.
import urllib
mystring = "î"
print urllib.quote(mystring)
print urllib.quote_plus(mystring)
print urllib.quote(mystring.encode('utf-8'))
Run Code Online (Sandbox Code Playgroud)
任何帮助赞赏.
我正在尝试处理的一些UTF-8文本有这个可爱的4字节字符:\ xF0\x9F\x98\xA5
根据这个网站,它是"失望但放心的脸":http://apps.timwhitlock.info/emoji/tables/unicode
在我看来,Python将其视为两个独立的角色.
这是我的测试代码:
mystring = '\xF0\x9F\x98\xA5'.decode('utf-8')
print len(mystring)
print mystring
print len(mystring.encode('utf-8'))
for c in mystring:
print c
Run Code Online (Sandbox Code Playgroud)
当我打印mystring时,我得到一张可爱的脸.但是当我打印出mystring的长度时,我得到2.
顺便说一句,我试图解决这个问题的原因是我需要在字符串中处理4个字节的字符,这样我就可以推送到5.5之前的MySQL数据库(它只能处理3个字节的UTF-8).
我很感激为什么Python似乎将其识别为两个字符,以及如何检测UTF-8字符串中的4字节字符.
谢谢.
我试图从列表形式存储的一组数据动态构建tkinter中的一些按钮.我无法解决的问题是如何在回调函数中添加一个参数.由于回调中的代码直到回调时才执行,因此回调中使用的变量此时已更改了值.
这是代码的(非常)简化版本:
from Tkinter import *
from ttk import *
mylist = ['a','b','c','d','e']
class App:
def __init__(self, master):
self.frame = Frame(master)
self.frame.pack()
for i in range (0,len(mylist)):
setattr(self, 'button' + str(i), Button(self.frame, text=mylist[i], command= lambda: self.buttoncall(i)))
getattr(self, 'button' + str(i)).pack(side=LEFT)
def buttoncall(self,input):
print mylist[input]
root = Tk()
app = App(root)
root.mainloop()
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用字符文字初始化后编辑字符串,如下所示:
int main() {
char str1[10] = "hello";
str1[0] = "b";
printf("%s\n", str1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果是"dello",即"d"而不是"b".同样,下面只是废话.
int main() {
char str1[10];
str1[0] = "h";
str1[1] = "e";
str1[2] = "l";
str1[3] = "l";
str1[4] = "o";
printf("%s\n", str1);
}
Run Code Online (Sandbox Code Playgroud)
我发现一个StackOverflow 帖子提到这应该导致段错误或访问冲突错误,但它并没有真正解释原因.