python-2.x

How do I case fold a string in Python 2?

时光总嘲笑我的痴心妄想 提交于 2019-12-29 05:20:53
问题 Python 3.3 adds the casefold method to the str type, but in 2.x I don't have anything. What's the best way to work around this? 回答1: Check out py2casefold. >>> from py2casefold import casefold >>> print casefold(u"tschüß") tschüss >>> casefold(u"ΣίσυφοςfiÆ") == casefold(u"ΣΊΣΥΦΟσFIæ") == u"σίσυφοσfiæ" True 回答2: There is a thread here which covers some of the issues (but may not resolve all), you can judge whether it is suitable for what you need. If this is no good then there are some useful

Division in Python 3 gives different result than in Python 2

社会主义新天地 提交于 2019-12-28 07:03:09
问题 In the following code, I want to calculate the percent of G and C characters in a sequence. In Python 3 I correctly get 0.5 , but on Python 2 I get 0 . Why are the results different? def gc_content(base_seq): """Return the percentage of G and C characters in base_seq""" seq = base_seq.upper() return (seq.count('G') + seq.count('C')) / len(seq) gc_content('attacgcg') 回答1: / is a different operator in Python 3; in Python 2 / alters behaviour when applied to 2 integer operands and returns the

How do you use subprocess.check_output() in Python?

邮差的信 提交于 2019-12-28 04:53:05
问题 I have found documentation about subprocess.check_output() but I cannot find one with arguments and the documentation is not very in depth. I am using Python 3 (but am trying to run a Python 2 file through Python 3) I am trying to run this command: python py2.py -i test.txt -i is a positional argument for argparse, test.txt is what the -i is, py2.py is the file to run I have tried a lot of (non working) variations including: py2output = subprocess.check_output([str('python py2.py '),'-i',

How to add an element to the beginning of an OrderedDict?

安稳与你 提交于 2019-12-28 04:52:05
问题 I have this: d1 = OrderedDict([('a', '1'), ('b', '2')]) If I do this: d1.update({'c':'3'}) Then I get this: OrderedDict([('a', '1'), ('b', '2'), ('c', '3')]) but I want this: [('c', '3'), ('a', '1'), ('b', '2')] without creating new dictionary. 回答1: There's no built-in method for doing this in Python 2. If you need this, you need to write a prepend() method/function that operates on the OrderedDict internals with O(1) complexity. For Python 3.2 and later, you should use the move_to_end method

Printing a string prints 'u' before the string in Python?

不想你离开。 提交于 2019-12-28 02:13:25
问题 'u' before elements in printed list? I didn't type u in my code. hobbies = [] #prompt user three times for hobbies for i in range(3): hobby = raw_input('Enter a hobby:') hobbies.append(hobby) #print list stored in hobbies print hobbies When I run this, it prints the list but it is formatted like this: Enter a hobby: Painting Enter a hobby: Stargazing Enter a hobby: Reading [u'Painting', u'Stargazing', u'Reading'] None Where did those 'u' come from before each of the elements of the list? 回答1:

How do convert unicode escape sequences to unicode characters in a python string

落花浮王杯 提交于 2019-12-28 01:55:23
问题 When I tried to get the content of a tag using "unicode(head.contents[3])" i get the output similar to this: "Christensen Sk\xf6ld". I want the escape sequence to be returned as string. How to do it in python? 回答1: Assuming Python sees the name as a normal string, you'll first have to decode it to unicode: >>> name 'Christensen Sk\xf6ld' >>> unicode(name, 'latin-1') u'Christensen Sk\xf6ld' Another way of achieving this: >>> name.decode('latin-1') u'Christensen Sk\xf6ld' Note the "u" in front

How do I retrieve stderr for a shell command with a large data buffer?

删除回忆录丶 提交于 2019-12-25 08:04:34
问题 Python subprocess module states regarding the communicate() function: Note The data read is buffered in memory, so do not use this method if the data size is large or unlimited. How can I execute a process that reads a lot of data (e.g. communicate() is contraindicated) and yet still have access to the stderr output? 回答1: To get possibly unlimited subprocess' stdout/stderr output separately as soon as it becomes available, you could use twisted spawnProcess(): #!/usr/bin/env python from

How would I nest file path strings into a list based upon matching folder paths in Python [duplicate]

旧城冷巷雨未停 提交于 2019-12-25 07:49:45
问题 This question already has answers here : List directory tree structure in python? (14 answers) Closed 2 years ago . I just asked a similar question, however this is a bit different. This time I'm trying to basically create a nested file tree structure in a list. Lets say I have this list: 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

Python Execute() takes exactly 2 arguments (3 given)

て烟熏妆下的殇ゞ 提交于 2019-12-25 04:05:52
问题 I am trying to insert into the SQLite DataBase values with this code: con.Execute('''UPDATE tblPlayers SET p_Level = ? WHERE p_Username= ? ''', (PlayerLevel,PlayerUsername)) this is the Execute function: def Execute(self,SQL): self.__connection.execute(SQL) self.__connection.comit() and i am getting this error: con.Execute('''UPDATE tblPlayers SET p_Level = ? WHERE p_Username= ? ''', (PlayerLevel,PlayerUsername)) TypeError: Execute() takes exactly 2 arguments (3 given) 回答1: Your Execute()

Print “Foo” if an element is in a list

左心房为你撑大大i 提交于 2019-12-25 02:22:45
问题 I have tried: >>> l = [1,2,3] >>> x = 1 >>> x in l and lambda: print("Foo") x in l && print "Horray" ^ SyntaxError: invalid syntax A bit of googling revealed that print is a statement in python2 whereas it's a function in python3 . But, I have tried the above snipped in python3 and it throws SyntaxError exception. Any idea on how can I do it in one line ? (Readability or google programming practice is not an issue here) 回答1: lambda creates, well a lambda. It needs to be called to execute it.