python-2.x

How to edit list elements in for loop – python

How to edit list elements in for loop – python Question: I’ve got a list of numbers and I’m trying to loop through the list and change each number depending on a certain criteria. But my code doesn’t change the list, its still the same when I print it again at the end. My code …

Total answers: 4

What versions of Python will work in Windows XP?

What versions of Python will work in Windows XP? Question: I would like the most advanced version of Python that still works on Windows XP. I need both Python 2 and Python 3. What versions of Python will work on Windows XP? Asked By: Fake Name || Source Answers: Any of them, python is very …

Total answers: 7

How to import a csv-file into a data array?

How to import a csv-file into a data array? Question: I have a line of code in a script that imports data from a text file with lots of spaces between values into an array for use later. textfile = open(‘file.txt’) data = [] for line in textfile: row_data = line.strip(“n”).split() for i, item in …

Total answers: 4

What is `1..__truediv__` ? Does Python have a .. ("dot dot") notation syntax?

What is `1..__truediv__` ? Does Python have a .. ("dot dot") notation syntax? Question: I recently came across a syntax I never seen before when I learned python nor in most tutorials, the .. notation, it looks something like this: f = 1..__truediv__ # or 1..__div__ for python 2 print(f(8)) # prints 0.125 I figured …

Total answers: 4

Better way to write a polling function in python

Better way to write a polling function in python Question: I wrote a polling function to check the value of reg_result variable for 120 seconds. reg_result = 0 while_timeout = time.time() + 120 while reg_result is not “REGISTERED” and time.time() < while_timeout: reg_result = LeshanObj.validate_reg_time(parameter_1) Is there any other better way of writing a polling …

Total answers: 1

Run interactive Bash with popen and a dedicated TTY Python

Run interactive Bash with popen and a dedicated TTY Python Question: I need to run an interactive Bash instance in a separated process in Python with it’s own dedicated TTY (I can’t use pexpect). I used this code snippet I commonly see used in similar programs: master, slave = pty.openpty() p = subprocess.Popen([“/bin/bash”, “-i”], stdin=slave, …

Total answers: 3

Python converting a list of strings to unicode

Python converting a list of strings to unicode Question: I have a list of strings that originally was a list of unicode element. And so, some string contains some character accent in unicode formate. list=[‘Cittxe0′,’Veszprxe9m’,’Publicitxe0′] I need to get a new list that looks like this: new_list=[u’Cittxe0′,u’Veszprxe9m’,u’Publicitxe0′] Each element of the new_list has to carry …

Total answers: 1

Python: How can I print bytes?

Python: How can I print bytes? Question: I’m using Python 2.7. I want to print a binary in decimal, but I’m receiving an error, which I do not understand. Eg. I am trying: print 0b111 I am expecting 7. But it returns: Unescaped left brace in regex is deprecated, passed through in regex; marked by …

Total answers: 1

Why can't I use a starred expression?

SyntaxError with starred expression when unpacking a tuple on its own for string formatting Question: I tried the following using the REPL in Python 3.5.2: >>> a = (1, 2) >>> ‘%d %d %d’ % (0, *a) ‘0 1 2’ >>> ‘%d %d %d’ % (*a, 3) ‘1 2 3’ >>> ‘%d %d’ % (*a) …

Total answers: 3