Python and BeautifulSoup encoding issues [duplicate]

In your case this page has wrong utf-8 data which confuses BeautifulSoup and makes it think that your page uses windows-1252, you can do this trick: soup = BeautifulSoup.BeautifulSoup(content.decode(‘utf-8′,’ignore’)) by doing this you will discard any wrong symbols from the page source and BeautifulSoup will guess the encoding correctly. You can replace ‘ignore’ by ‘replace’ … Read more

ipdb debugger, step out of cycle

I believe this is the intent of the until command. It’s like a next except that when a jump occurs to a previous line number for the loop, it will continue until exiting the loop. unt(il) Continue execution until the line with a number greater than the current one is reached or until the current … Read more

how to write to a new cell in python using openpyxl

Try this: import openpyxl wb = load_workbook(filename=”xxxx.xlsx”) ws = wb.worksheets[0] ws[‘A1’] = 1 ws.cell(row=2, column=2).value = 2 This will set Cells A1 and B2 to 1 and 2 respectively (two different ways of setting cell values in a worksheet). The second method (specifying row and column) is most useful for your situation: import openpyxl wb … Read more

Find dictionary keys with duplicate values

First, flip the dictionary around into a reverse multidict, mapping each value to all of the keys it maps to. Like this: >>> some_dict = {“firstname”:”Albert”,”nickname”:”Albert”,”surname”:”Likins”,”username”:”Angel”} >>> rev_multidict = {} >>> for key, value in some_dict.items(): … rev_multidict.setdefault(value, set()).add(key) Now, you’re just looking for the keys in the multidict that have more than 1 value. … Read more

Does the extra comma at the end of a dictionary, list or set has any special meaning in Python?

It has no special meaning in a list or dictionary, but can be useful when using source code change management tools, see below. Non-empty tuples are defined by using a comma between elements, the parentheses are optional and only required in contexts where the comma could have a different meaning. Because the comma defines the … Read more

How to change the point size for regplot(), seaborn’s scatter plot function (python)

To do this you can feed the regplot() function the scatter_kws arg like so: import seaborn as sns tips = sns.load_dataset(‘tips’) sns.regplot(x=’total_bill’, y=’tip’, data=tips, marker=”o”, color=”red”, scatter_kws={‘s’:2}) sns.regplot(x=’total_bill’, y=’tip’, data=tips, marker=”o”, color=”red”, scatter_kws={‘s’:20})

Grouped Bar graph Pandas

Using pandas: import pandas as pd groups = [[23,135,3], [123,500,1]] group_labels = [‘views’, ‘orders’] # Convert data to pandas DataFrame. df = pd.DataFrame(groups, index=group_labels).T # Plot. pd.concat( [ df.mean().rename(‘average’), df.min().rename(‘min’), df.max().rename(‘max’) ], axis=1, ).plot.bar()

File not found.