[Solved] Getting all possible combination for [1,0] with length 3 [0,0,0] to [1,1,1]

You’re looking for a Cartesian product, not a combination or permutation of [0, 1]. For that, you can use itertools.product. from itertools import product items = [0, 1] for item in product(items, repeat=3): print(item) This produces the output you’re looking for (albeit in a slightly different order): (0, 0, 0) (0, 0, 1) (0, 1, … Read more

[Solved] python url extract from html

Observe Python 2.7.3 (default, Sep 4 2012, 20:19:03) [GCC 4.2.1 20070831 patched [FreeBSD]] on freebsd9 Type “help”, “copyright”, “credits” or “license” for more information. >>> junk=”’ <a href=””http://a0c5e.site.it/r”” target=_blank><font color=#808080>MailUp</font></a> … <a href=””http://www.site.it/prodottiLLPP.php?id=1″” class=””txtBlueGeorgia16″”>Prodotti</a> … <a href=””http://www.site.it/terremoto.php”” target=””blank”” class=””txtGrigioScuroGeorgia12″”>Terremoto</a> … <a class=”mini” href=”http://www.site.com/remove/professionisti.aspx?Id=65&Code=xhmyskwzse”>clicca qui.</a>`”’ >>> import re >>> pat=re.compile(r”’http[\:/a-zA-Z0-9\.\?\=&]*”’) >>> pat.findall(junk) [‘http://a0c5e.site.it/r’, ‘http://www.site.it/prodottiLLPP.php?id=1’, ‘http://www.site.it/terremoto.php’, ‘http://www.site.com/remove/professionisti.aspx?Id=65&Code=xhmyskwzse’] … Read more

[Solved] How to remove \r\n in command prompt after running?

strip() can remove \r\n only at the end of string, but not inside. If you have \r\n inside text then use text = text.replace(\r\n’, ”) it seems you get \r\n in list created by extract() so you have to use list comprehension to remove from every element on list data = response.css(find).extract() data = [x.replace(‘\r\n’, … Read more

[Solved] Counting a specific string in the list in Python

You need to have the count variable inside for loop, and you dont require an else condition def fizz_count(x): count=0 for string in x: if string == ‘fizz’: count = count + 1 return count You can also write your function as def fizz_count(x): return x.count(‘fizz’) solved Counting a specific string in the list in … Read more

[Solved] How do I loop through a single data frame column to count how many different values there are?

you have to use df.unique(). That’s why pandas is used. You are not supposed to loop through pandas. Even for applying manipulations to the data inside dataframe you should go for df.apply(). This should get the list of unique date values as a list: df.Date.unique() In case, you want to use loop: Use df.iterrows() : … Read more

[Solved] Web Server Block libwww-perl requests

Use agent method provided by LWP::UserAgent to change “user agent identification string”. It should solve blocking based on client identification string. It will not solve blocking based on abusive behavior. perldoc LWP::UserAgent agent my $agent = $ua->agent; $ua->agent(‘Checkbot/0.4 ‘); # append the default to the end $ua->agent(‘Mozilla/5.0’); $ua->agent(“”); # don’t identify Get/set the product token … Read more

[Solved] Python Unique DF in loop [closed]

If you need to upload into csv files separately during that iteration then below code follows, If this is what your looking or else please review your question for j in range(0, 500): for k in range(1,9): try: file_name=”Result_”+str(i)+’_’+str(j)+’.csv’ df1 = url_base_1 + str(j) + url_base_2 df2 = make_dataframe(df1.format(year), int(k)) print(k) df2.to_csv(file_name,encoding=’utf-8′, index=False) except: pass … Read more

[Solved] Python – Check if a word is in a string [duplicate]

What about to split the string and strip words punctuation, without forget the case? w in [ws.strip(‘,.?!’) for ws in p.split()] Maybe that way: def wordSearch(word, phrase): punctuation = ‘,.?!’ return word in [words.strip(punctuation) for words in phrase.split()] # Attention about punctuation (here ,.?!) and about split characters (here default space) Sample: >>> print(wordSearch(‘Sea’.lower(), ‘Do … Read more

[Solved] parse a HTML file with table using Python

Find all tr tags and get td tags by class attribute: # encoding: utf-8 from bs4 import BeautifulSoup data = u””” <table> <tr> <td class=”zeit”><div>03.12. 10:45:00</div></td> <td class=”system”><div><a target=”_blank” href=”https://stackoverflow.com/questions/27272247/detail.php?host=CG&factor=2&delay=1&Y=15″>CG</div></a></td> <td class=”fehlertext”><div>System steht nicht zur Verfügung!</div></td> </tr> <tr> <td class=”zeit”><div>03.12. 10:10:01</div></td> <td class=”system”><div><a target=”_blank” href=”detail.php?host=DEXProd&factor=2&delay=5&Y=15″>DEX</div></a></td> <td class=”fehlertext”><div>ssh: Connection refused Couldn’t read packet: Connection reset by … Read more

[Solved] Index lists for specific repeating element

The following is a much shorter and solution, and might be preferable – main_list = [True, True, False, False, True, True, True, True, True, False] def my_diff(my_list): return [1 if my_list[0] else 0] + [y – x for x, y in zip(my_list[:-1], my_list[1:])] solution = [i for i, x in enumerate(my_diff(main_list)) if x == 1] … Read more

[Solved] Regex replace `a.b` to `a. b`? [duplicate]

You can use the following ([abAB])\.([abAB]) >>> import re >>> s = [‘a.b’, ‘A.b’, ‘a.B’, ‘A.B’] >>> [re.sub(r'([abAB])\.([abAB])’, r’\1. \2′, i) for i in s] [‘a. b’, ‘A. b’, ‘a. B’, ‘A. B’] The issue with your approach is that you are not saving capturing groups to use in the result of the substitution. If … Read more

[Solved] Web scraping program cannot find element which I can see in the browser

The element you’re interested in is dynamically generated, after the initial page load, which means that your browser executed JavaScript, made other network requests, etc. in order to build the page. Requests is just an HTTP library, and as such will not do those things. You could use a tool like Selenium, or perhaps even … Read more