Questions tagged «python-3.x»

对于特定于该语言版本3+的有关Python编程的问题。如果您的问题不是特定于版本的,则使用更通用的[python]标记。将[python-2.x]标记用于Python 2问题。

12
在Python中连接字符串的首选方法是什么?
由于string无法更改Python ,因此我想知道如何更有效地连接字符串? 我可以这样写: s += stringfromelsewhere 或像这样: s = [] s.append(somestring) later s = ''.join(s) 在写这个问题时,我找到了一篇很好的文章,谈论这个话题。 http://www.skymind.com/~ocrow/python_string/ 但是它在Python 2.x中,所以问题是在Python 3中会有所改变吗?


5
Python中字典的深层副本
我想dict在python中制作一个深层副本。不幸的是,该.deepcopy()方法不存在dict。我怎么做? >>> my_dict = {'a': [1, 2, 3], 'b': [4, 5, 6]} >>> my_copy = my_dict.deepcopy() Traceback (most recent calll last): File "<stdin>", line 1, in <module> AttributeError: 'dict' object has no attribute 'deepcopy' >>> my_copy = my_dict.copy() >>> my_dict['a'][2] = 7 >>> my_copy['a'][2] 7 最后一行应为3。 我希望所做的修改my_dict不会影响快照my_copy。 我怎么做?该解决方案应与Python 3.x兼容。
340 python  python-3.x 

8
使用Python 3从网上下载文件
我正在创建一个程序,该程序将通过读取同一游戏/应用程序的.jad文件中指定的URL从Web服务器下载.jar(java)文件。我正在使用Python 3.2.1 我设法从JAD文件中提取JAR文件的URL(每个JAD文件都包含指向JAR文件的URL),但是正如您所想象的,提取的值是type()字符串。 相关功能如下: def downloadFile(URL=None): import httplib2 h = httplib2.Http(".cache") resp, content = h.request(URL, "GET") return content downloadFile(URL_from_file) 但是,我总是得到一个错误,指出上面函数中的类型必须是字节,而不是字符串。我尝试使用URL.encode('utf-8')和字节(URL,encoding ='utf-8'),但是我总是会遇到相同或相似的错误。 因此,基本上我的问题是,当URL以字符串类型存储时,如何从服务器下载文件?
332 python  python-3.x 


18
列出对象的属性
有没有办法获取类实例上存在的属性列表? class new_class(): def __init__(self, number): self.multi = int(number) * 2 self.str = str(number) a = new_class(2) print(', '.join(a.SOMETHING)) 理想的结果是将输出“ multi,str”。我希望它可以查看脚本各个部分的当前属性。
329 python  class  python-3.x 

16
Python 3 ImportError:没有名为“ ConfigParser”的模块
我想pip install的MySQL-python包,但我得到的ImportError。 Jans-MacBook-Pro:~ jan$ /Library/Frameworks/Python.framework/Versions/3.3/bin/pip-3.3 install MySQL-python Downloading/unpacking MySQL-python Running setup.py egg_info for package MySQL-python Traceback (most recent call last): File "<string>", line 16, in <module> File "/var/folders/lf/myf7bjr57_jg7_5c4014bh640000gn/T/pip-build/MySQL-python/setup.py", line 14, in <module> from setup_posix import get_config File "./setup_posix.py", line 2, in <module> from ConfigParser import SafeConfigParser ImportError: No module named …
322 python  python-3.x 


6
小数点后带有f字符串的固定数字
Python f字符串是否有一种简单的方法来固定小数点后的位数?(特别是f字符串,不是其他字符串格式设置选项,例如.format或%) 例如,假设我要在小数点后两位显示数字。 我怎么做?比方说 a = 10.1234

11
在Python中打印多个参数
这只是我的代码的一部分: print("Total score for %s is %s ", name, score) 但我希望它打印出来: “(姓名)的总分是(分数)” 其中name是列表中的变量,score是整数。如果有帮助的话,这就是Python 3.3。

6
NameError:全局名称“ xrange”未在Python 3中定义
运行python程序时出现错误: Traceback (most recent call last): File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 110, in <module> File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 27, in __init__ File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\class\inventory.py", line 17, in __init__ builtins.NameError: global name 'xrange' is not defined 游戏是从这里开始的。 是什么导致此错误?

10
如何更正TypeError:散列之前必须对Unicode对象进行编码?
我有这个错误: Traceback (most recent call last): File "python_md5_cracker.py", line 27, in <module> m.update(line) TypeError: Unicode-objects must be encoded before hashing 当我尝试在Python 3.2.2中执行以下代码时: import hashlib, sys m = hashlib.md5() hash = "" hash_file = input("What is the file name in which the hash resides? ") wordlist = input("What is your wordlist? …

6
'else if'的正确语法是什么?
我是一名新的Python程序员,他正在从2.6.4跃升至3.1.1。在尝试使用“ else if”语句之前,一切都进行得很好。解释器在“ else if”中的“ if”之后给了我一个语法错误,原因是我似乎无法弄清。 def function(a): if a == '1': print ('1a') else if a == '2' print ('2a') else print ('3a') function(input('input:')) 我可能缺少一些非常简单的东西;但是,我无法自行找到答案。
284 python  python-3.x 

18
是否有用于字符串自然排序的内置函数?
使用Python 3.x,我有一个要对其执行自然字母排序的字符串列表。 自然排序: Windows中文件的排序顺序。 例如,以下列表是自然排序的(我想要的): ['elm0', 'elm1', 'Elm2', 'elm9', 'elm10', 'Elm11', 'Elm12', 'elm13'] 这是上面列表的“排序”版本(我所拥有的): ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] 我正在寻找一种类似于第一个的排序功能。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.