python-2.7

Use soup.get_text() with UTF-8

旧时模样 提交于 2021-02-18 11:40:35
问题 I need to get all the text from a page using BeautifulSoup. At BeautifulSoup's documentation, it showed that you could do soup.get_text() to do this. When I tried doing this on reddit.com, I got this error: UnicodeEncodeError in soup.py:16 'cp932' codec can't encode character u'\xa0' in position 2262: illegal multibyte sequence I get errors like that on most of the sites I checked. I got similar errors when I did soup.prettify() too, but I fixed it by changing it to soup.prettify('UTF-8') .

r“string” b“string” u“string” Python 2 / 3 comparison

旧巷老猫 提交于 2021-02-18 11:10:50
问题 I already know r"string" in Python 2.7 often used for regex patterns. I also have seen u"string" for, I think, Unicode strings. Now with Python 3 we see b"string" . I have searched for these in different sources / questions, such as What does a b prefix before a python string mean?, but it's difficult to see the big picture of all these strings with prefixes in Python, especially with Python 2 vs 3. Question: would you have a rule of thumb to remember the different types of strings with

Different behaviour of ctypes c_char_p?

倖福魔咒の 提交于 2021-02-18 09:51:13
问题 I am confused with this behaviour of different versions of python and dont understand why ? Python 2.7.5 (default, Aug 25 2013, 00:04:04) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> c="hello" >>> a=ctypes.c_char_p(c) >>> print(a.value) hello Python 3.3.5 (default, Mar 11 2014, 15:08:59) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin Type "help", "copyright", "credits" or "license"

Python Turtle Graphics Keyboard Commands

ぐ巨炮叔叔 提交于 2021-02-18 08:37:40
问题 Anybody have any insight into controlling turtle graphics in python 2.7 with keyboard commands? I have done extensive research on this website and others and feel like I am doing the right thing but it just doesn't want to work for me. Below is what I have so far, can anyone tell me where I am going wrong???? from turtle import * turtle.setup(500, 500) wn = turtle.Screen() wn.title("Turtle Keys") move = turtle.Turtle() showturtle() def k1(): move.forward(45) def k2(): move.left(45) def k3():

TreeView in Python+QT

家住魔仙堡 提交于 2021-02-18 08:37:28
问题 I need to make a treeView with 4 columns with a checkbox in the first column. I have made ​​the tree view, just that I do not put the checkbox in the first column. I tried but it gets me in every position (row, column ) ........... Here is my code: import sys from PyQt4.QtCore import * from PyQt4.QtGui import * from copy import deepcopy from cPickle import dumps, load, loads from cStringIO import StringIO class myNode(object): def __init__(self, name, state, description,otro, parent=None

Exception in thread “main” java.lang.UnsatisfiedLinkError: no jep in java.library.path

若如初见. 提交于 2021-02-18 08:13:11
问题 I have 'libjep.so' file after downloading jep and I also had set the environmental variable LD_LIBRARY_PATH in ~./bashrc as shown below: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/python2.7/dist-packages/jep/libjep.so as well as in runtime System.load("/usr/local/lib/python2.7/dist-packages/jep/libjep.so"); But when I have the follwing line in my code, Jep jep = new Jep(); It shows the below error: Exception in thread "main" java.lang.UnsatisfiedLinkError: no jep in java.library

Make tkinter buttons for every item in a list?

隐身守侯 提交于 2021-02-18 07:56:26
问题 I would like to make some buttons, with a list of items I get back from a database, that all call a function passing in the list item. Something like this code but that works. The problem with this code is that all of the buttons call the function with 'item3' . #!/usr/bin/env python from Tkinter import * root = Tk() def func(name): print name mylist = ['item1','item2','item3'] for item in mylist: button = Button(root,text=item,command=lambda:func(item)) button.pack() root.mainloop() 回答1:

Using Beautiful Soup with accents and different characters

最后都变了- 提交于 2021-02-18 07:44:11
问题 I'm using Beautiful Soup to pull medal winners from past Olympics. It's tripping over the use of accents in some of the events and athlete names. I've seen similar problems posted online but I'm new to Python and having trouble applying them to my code. If I print my soup, the accents appear fine. but when I start parsing the soup (and write it to a CSV file) the accented characters become garbled. 'Louis Perrée' becomes 'Louis Perr√©e' from BeautifulSoup import BeautifulSoup import urllib2

Combine multiple csv files into a single xls workbook Python 3

两盒软妹~` 提交于 2021-02-18 06:49:10
问题 We are in the transition at work from python 2.7 to python 3.5. It's a company wide change and most of our current scripts were written in 2.7 and no additional libraries. I've taken advantage of the Anaconda distro we are using and have already change most of our scripts over using the 2to3 module or completely rewriting them. I am stuck on one piece of code though, which I did not write and the original author is not here. He also did not supply comments so I can only guess at the whole of

How to share the same instance for all methods of a pytest test class

雨燕双飞 提交于 2021-02-18 06:47:27
问题 I have a simple test class @pytest.mark.incremental class TestXYZ: def test_x(self): print(self) def test_y(self): print(self) def test_z(self): print(self) When I run this I get the following output: test.TestXYZ object at 0x7f99b729c9b0 test.TestXYZ object at 0x7f99b7299b70 testTestXYZ object at 0x7f99b7287eb8 This indicates that the 3 methods are called on 3 different instances of TestXYZ object. Is there anyway to change this behavior and make pytest call all the 3 methods on the same