python-2.x

python 2 and 3 extract domain from url

孤街浪徒 提交于 2020-01-14 07:11:36
问题 I have an url like: http://xxx.abcdef.com/fdfdf/ And I want to get xxx.abcdef.com Which module can i use for accomplish this? I want to use the same module and method at python2 and python3 I don't like the try except way for python2/3 compatibility Thanks you so much! 回答1: Use urlparse: from urlparse import urlparse o = urlparse("http://xxx.abcdef.com/fdfdf/") print o print o.netloc In Python 3, you import urlparse like so: from urllib.parse import urlparse Alternatively, just use str.split(

Associativity of “in” in Python?

我是研究僧i 提交于 2020-01-11 15:05:46
问题 I'm making a Python parser, and this is really confusing me: >>> 1 in [] in 'a' False >>> (1 in []) in 'a' TypeError: 'in <string>' requires string as left operand, not bool >>> 1 in ([] in 'a') TypeError: 'in <string>' requires string as left operand, not list How exactly does "in" work in Python, with regards to associativity, etc.? Why do no two of these expressions behave the same way? 回答1: 1 in [] in 'a' is evaluated as (1 in []) and ([] in 'a') . Since the first condition ( 1 in [] ) is

How do I prevent my Python application from automatically closing once reaching the end of code? [duplicate]

本秂侑毒 提交于 2020-01-11 09:45:09
问题 This question already has answers here : How to keep a Python script output window open? (20 answers) Closed 5 years ago . I'm new to programming, especially Python. I'm trying to make an application that converts Fahrenheit to Celsius, but I don't know how to make the program stay open. Whenever it reaches the end of the code, it automatically closes before the user can see his or her results. I'm using Python 2.6. 回答1: Well, I guess you mean the terminal that Windows opens for you when you

Decode string with hex characters in python 2

匆匆过客 提交于 2020-01-11 05:56:12
问题 I have a hex string and i want to convert it utf8 to insert mysql. (my database is utf8) hex_string = 'kitap ara\xfet\xfdrmas\xfd' ... result = 'kitap araştırması' How can I do that? 回答1: Assuming Python 2.6, >>> print('kitap ara\xfet\xfdrmas\xfd'.decode('iso-8859-9')) kitap araştırması >>> 'kitap ara\xfet\xfdrmas\xfd'.decode('iso-8859-9').encode('utf-8') 'kitap ara\xc5\x9ft\xc4\xb1rmas\xc4\xb1' 回答2: Try(Python 3.x): import codecs codecs.decode("707974686f6e2d666f72756d2e696f", "hex").decode(

What is the largest number the Decimal class can handle?

老子叫甜甜 提交于 2020-01-11 04:42:09
问题 My program calculates the mathematical constant e , which is irrational. In order to do this, I needed to get factorials of very large numbers. int cannot handle numbers larger than 170!. (I found that the largest Google's calculator can handle is 170.654259, but I'm not sure how a non integer can be factorized.) float can not handle very large numbers either. I calculated e to 750000 digits, and math.factorial(750000) is a mind-boggling, large number. Yet, Decimal handled it with apparent

Python, mixing PyQt5 and abc.ABCMeta

本小妞迷上赌 提交于 2020-01-10 06:04:26
问题 I am trying to create an AbstractClass using both abc.ABCMeta and QObject as parents and cannot seems to make it work. Here is the Base class init. I have Pyqt5 and python 2.7 pyqtWrapperType = type(QObject) class ParamsHandler(abc.ABCMeta, pyqtWrapperType): def __init__(self, device_model, read_only=False): super(ParamsHandler, self).__init__() self.cmd_to_get_data = None self.device_model = device_model class ConfigParamsHandler(ParamsHandler): def __init__(self, device_model): super

Python How to get every first element in 2 Dimensional List

落花浮王杯 提交于 2020-01-09 13:14:24
问题 I have a list like this : a = ((4.0, 4, 4.0), (3.0, 3, 3.6), (3.5, 6, 4.8)) I want an outcome like this ( EVERY first element in the list) : 4.0, 3.0, 3.5 I tried a[::1][0], but it doesn't work I'm just start learning Python weeks ago. Python version = 2.7.9 回答1: You can get the index [0] from each element in a list comprehension >>> [i[0] for i in a] [4.0, 3.0, 3.5] Also just to be pedantic, you don't have a list of list , you have a tuple of tuple . 回答2: use zip columns = zip(*rows)

How do I convert a unicode to a string at the Python level?

假如想象 提交于 2020-01-09 10:01:51
问题 The following unicode and string can exist on their own if defined explicitly: >>> value_str='Andr\xc3\xa9' >>> value_uni=u'Andr\xc3\xa9' If I only have u'Andr\xc3\xa9' assigned to a variable like above, how do I convert it to 'Andr\xc3\xa9' in Python 2.5 or 2.6? EDIT: I did the following: >>> value_uni.encode('latin-1') 'Andr\xc3\xa9' which fixes my issue. Can someone explain to me what exactly is happening? 回答1: You seem to have gotten your encodings muddled up. It seems likely that what

Reading binary data from stdin

吃可爱长大的小学妹 提交于 2020-01-08 17:15:29
问题 Is it possible to read stdin as binary data in Python 2.6? If so, how? I see in the Python 3.1 documentation that this is fairly simple, but the facilities for doing this in 2.6 don't seem to be there. If the methods described in 3.1 aren't available, is there a way to close stdin and reopen in in binary mode? Update Just to be clear, I am using 'type' in a MS-DOS shell to pipe the contents of a binary file to my python code. This should be the equivalent of a Unix 'cat' command, as far as I

Can os.walk() be used on a list of strings? If not, what would be the equivalent?

試著忘記壹切 提交于 2020-01-06 19:54:37
问题 Instead of using a real file structure, is it possible to give it a premade list of strings to have it create a nested list of paths/files for you? Example List (formatted so it's readable): files = [ 'user/hey.jpg', 'user/folder1/1.txt', 'user/folder1/folder2/random.txt', 'user/folder1/blah.txt', 'user/folder3/folder4/folder5/1.txt', 'user/folder3/folder4/folder5/3.txt', 'user/folder3/folder4/folder5/2.txt', 'user/1.jpg' ] Here is the output I'm hoping for: ['user'['1.jpg','hey.jpg','folder1