[Solved] Method which would prompt the user for input and then output that same input, replacing each space with three periods (…) [closed]

expandtabs() is used for setting the tab size and not replacing the spaces. However, you can use str.replace() to replace any given substring with another. txt = input(“Please type in your text here “) txt = txt.replace(‘ ‘, ‘…’) print(txt) solved Method which would prompt the user for input and then output that same input, … Read more

[Solved] Trouble with Personal project code (replacing characters) [closed]

In python, tuples are defined with tup = (val1,val2,…). This is what you’re doing when you define wholeAssDocumentation. Since tuples don’t have a re method, you’re getting that error. Other than that, I suggest you read the regex documentation because that’s not how you use a regex on a string either. Also you’re overwriting the … Read more

[Solved] How to Enter a sequence of alphabet in 9×9 matrix and then the alphabet put values in that matrix to create an image in python, how do i do that? [closed]

Here is an example of how to do what you are asking: # Create string of 81 uppercase letters string = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’*3 + ‘ABC’ # Assign each letter a number, and scale the numbers from 0 to 1 num = [(ord(string[char]) – 65)/25 for char in range(len(string))] # Assemble the numbers into a 9×9 matrix … Read more

[Solved] list comprehension remove 2d list (sublist) from list if length is less than 7 [closed]

For each value in the list, return it, if all the elements of the sublist have length less-than-or-equal-to 6 [x for x in mylist if all(len(y) <= 6 for y in x)] You can also create a list from the singular string elements, but unclear why you’d want this >>> simple_list=[‘apple’, ‘banana’, ‘cantaloupe’, ‘durian’] >>> … Read more

[Solved] Scraping data from a dynamic web database with Python [closed]

You can solve it with requests (for maintaining a web-scraping session) + BeautifulSoup (for HTML parsing) + regex for extracting a value of a javascript variable containing the desired data inside a script tag and ast.literal_eval() for making a python list out of js list: from ast import literal_eval import re from bs4 import BeautifulSoup … Read more

[Solved] Please change your browser settings or upgrade your browser ( urllib, python 3.6) [closed]

The site you are accessing requires javascript and cookies. Urllib only provides support for cookies (with cookie jar). To get around the need for javascript, check out a library like selenium, and use it with phantomjs. pip install selenium Selenium with phantomjs is more powerful then urllib, but you pay the penalty of it being … Read more

[Solved] How to remove the last comma?

If you want to do it your way: for i in range(1, 21): print(i, end=”,” if i!=20 else “” ) Output: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 But a better way of doing this would be: print(*range(1, 21), sep=”,”) solved How to remove the last comma?

[Solved] Why my recursion doesn’t work in os.listdir(), it doesn’t go to the lower level of a folder

Your problem stems from poorly thought out and/or formatted path strings. Instead of concatenating raw strings, use os.path.join(). And make sure you’re always passing absolute paths. In that same vein, using string methods directly instead of “+” is often more efficient and usually more readable. With those in mind, working code: import os def rec(direc, … Read more

[Solved] When should give ax.imshow() a variable name? [closed]

You should use im = ax.imshow() when you want add something to the existing axis, for example, a colorbar. If you don’t need to add anything to the axis, then it is fine to just use ax.imshow(). See this: https://www.geeksforgeeks.org/matplotlib-axes-axes-imshow-in-python/ Example from the link: c = ax.imshow(z, cmap =’Greens’, vmin = z_min, vmax = z_max, … Read more

[Solved] How to get rid of a list in Python when the element value of a list is a list [closed]

Try: a = [(‘aa’, ‘bb’, [‘cc’, ‘dd’]), (‘ee’, ‘ff’, [‘gg’, ‘ff’])] def flatten(tup): lst = [] for x in tup: if isinstance(x, list): lst += x else: lst.append(x) return tuple(lst) output = [flatten(t) for t in a] print(output) # [(‘aa’, ‘bb’, ‘cc’, ‘dd’), (‘ee’, ‘ff’, ‘gg’, ‘ff’)] As you pointed out, applying How to make … Read more