python-3.2

How to read an array of integers from single line of input in python3

How to read an array of integers from single line of input in python3 Question: I want to read an array of integers from single line of input in python3. For example: Read this array to a variable/list 1 3 5 7 9 What I have tried arr = input.split(‘ ‘) But this does not …

Total answers: 5

Call to GetModuleHandle on kernel32 using Python C-types

Call to GetModuleHandle on kernel32 using Python C-types Question: I’m trying to use Python’s C-Types to call GetModuleHandleA on the kernel32 library. I’d like to get a handle to the library so I can use it to call GetProcAddress for LoadLibraryA. Below is my code… import sys from ctypes kernel32 = windll.kernel32 print(“The kernel32 is …

Total answers: 2

PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseam)

PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseam) Question: Basically there seems to be massive confusion/ambiguity over when exactly PyEval_InitThreads() is supposed to be called, and what accompanying API calls are needed. The official Python documentation is unfortunately very ambiguous. There are already many questions on stackoverflow regarding this topic, …

Total answers: 7

Python center string using format specifier

Python center string using format specifier Question: I have a string called message. message = “Hello, welcome!nThis is some text that should be centered!” And I’m trying to center it for a default Terminal window, i.e. of 80 width, with this statement: print(‘{:^80}’.format(message)) Which prints: Hello, welcome! This is some text that should be centered! …

Total answers: 2

Start IDLE with Python 3 on Linux (Python 2.7 installed alongside)

Start IDLE with Python 3 on Linux (Python 2.7 installed alongside) Question: I initially had Python 2.7, which often comes bundled with the OS (currently using Linux Mint 12). I wanted to try Python 3, so I installed it alongside Python 2. All is fine there, and I can run both versions in the terminal by calling …

Total answers: 6

python 3.2 error saying urllib.parse.urlencode() is not defined

python 3.2 error saying urllib.parse.urlencode() is not defined Question: I am trying to use urllib.parse.urlencode() method in one of my scripts. import urllib #!/usr/bin/python3.2 import urllib data = urllib.parse.urlencode({‘type’: ‘device_code’,’client_id’: 150792241632891}) It was working before but now I get following error. Output Traceback (most recent call last): File “/home/rakesh/programming/test.py”, line 8, in <module> data = …

Total answers: 1

Python dictionary.keys() error

Python dictionary.keys() error Question: I am trying to use the .keys() and instead of getting a list of the keys like always have in the past. However I get this. b = { ‘video’:0, ‘music’:23 } k = b.keys() print( k[0] ) >>>TypeError: ‘dict_keys’ object does not support indexing print( k ) dict_keys([‘music’, ‘video’]) it …

Total answers: 5

Usage of pickle.dump in Python

Usage of pickle.dump in Python Question: I’m trying to learn how to use the pickle module in Python: import pickle x = 123 f = open(‘data.txt’,’w’) pickle.dump(x,f) Here’s what I get: Traceback (most recent call last): File “D:pythontest.py”, line 5, in <module> pickle.dump(x,f) TypeError: must be str, not bytes However, this code works just fine: …

Total answers: 2

NameError: name 'reduce' is not defined in Python

NameError: name 'reduce' is not defined in Python Question: I’m using Python 3.2. Tried this: xor = lambda x,y: (x+y)%2 l = reduce(xor, [1,2,3,4]) And got the following error: l = reduce(xor, [1,2,3,4]) NameError: name ‘reduce’ is not defined Tried printing reduce into interactive console – got this error: NameError: name ‘reduce’ is not defined …

Total answers: 7