Code Answers

Python – TypeError: list indices must be integers, not str

The declaration of the variable used is as follows: self.features = {} #dictionary defined for storing the features and the values self.featureNameList = [] #list to store the names and values of the features self.featureCounts = collections.defaultdict(lambda: 1) #the counts of the features and labels self.featureVectors = [] # self.l...

Python regex not working

I am using the following code: downloadlink = re.findall("http://uploadir.com/u/(.*)\b", str(downloadhtml)) However, when I pass it the following string: It finds nothing, when I'm expecting it to find: http://uploadir.com/u/bb41c5b3. What am I doing wrong? I have tested the regex using http://gskinner.com/RegExr/ and it seems to be correct....

Python re ?: example

i saw a regular expression (?= (?:\d{5}|[A-Z]{2})) in a python re example, and was very confused about the meaning of the ?: . I also see the python doc, there is the explain: (?:...) A non-capturing version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group cannot be...

Python – maximum recursion depth exceeded while calling a python object

My aim is to instantiate a class called arrow, so I can have more arrows then just 1. I wanted to start with the coordinates 200, 200 and want to increase x by 15 per every 100 milliseconds. But when I try to execute this code it gives me following error: File "game.py", line 25, in moveArrow self.after(100, self.moveArrow(arrow, xCoord+1...

Python – EOL while scanning string literal

this is my code and get a following error message: line 8 sepFile=readFile.read().split('\') SyntaxError: EOL while scanning string literal could you help me? Thanks. import matplotlib.pyplot as plt import numpy as np x=[] y=[] readFile = open (("/Users/Sun/Desktop/text58.txt"), 'r') sepFile=readFile.read().split('\') readFile.close() For pl...

Python – the difference between ‘ ‘ and ” ” in python?

Possible Duplicates: Is there any difference between “string” and 'string' in Python? Single quotes vs. double quotes in Python I have noted that I can use both single and double quotes to delimit strings in python, and that whichever one I don't use to delimit the string, I can use freely inside it without any need to escape it. Examples:...

Python indentation error:

I have tried notepad++ and eclipse but even then , it is showing me an indentation error at line 18. I don't know, why it is throwing me an error like that…? please help me. from brisa.core.reactors.qtreactor import QtReactor reactor = QtReactor() from brisa.core import config from brisa.upnp.device import Device from brisa.upnp.device.service ...

Python – TypeError: ‘NoneType’ object does not support item assignment

So I start a root screen with a "file select" and a "go" button. The go button is disabled and I want to make it active after the file has been selected. When I select the file go should become active but instead this error "TypeError: 'NoneType' object does not support item assignment" Here is some sample code import Tkinter import tkFileDial...

Python – sqlite3.OperationalError: near “,”: syntax error

Update 1: The other line works (Thanks for the help!). Now i tried to run the following, and this wont work either, i get an error for line 13: sqlite3.OperationalError: near ",": syntax error the code is as follows: cur.execute("CREATE TABLE Progress(Id INTEGER PRIMARY, Username STRING, Topic1 STRING, Topic2 STRING, Topic3 STRING, Topic4 STR...

Python: TypeError: takes exactly 1 argument (2 given)

I'm currently using singpath.com to practice my python, but I face an issue with a problem: The expected result is: >>>CurryPuff(3) 3.60 >>>CurryPuff(3,'Fish') 4.2 This is something I tried: def CurryPuff(x,typePuff): if(typePuff==''): return x*1.2 if(typePuff=='Fish'): return x*1.4 But it give me this error: TypeErro...

Python ValueError: unconverted data remains:

I have the following code: This is my Code: print (start_timestamp) start_timestamp_no_iso = datetime.strptime(start_timestamp, "%Y-%m-%dT%H:%M:%S.%f") This is what I get: INFO - 2018-11-20T14:44:03.452131 INFO - Traceback (most recent call last): INFO - File "/home/ubuntu/script.py", line 84, in INFO - start_timestamp_no_iso = datetime.strpt...

Python subprocess output without \n

Here is a simple script running subprocess that retrieves IP from the ifconfig command output from the terminal. I have noticed that subprocess.check_output() always returns a value with \n. I desire to get a return value without \n. How can this be done? $ python >>> import subprocess >>> subprocess.check_output("ifconfig en0 | awk '{ print $2...

Python SQLite how to get SQL string statement being executed

Let's say we have a SQL statement that just needs to be completed with the parameters before getting executed against the DB. For instance: sql = ''' SELECT id, price, date_out FROM sold_items WHERE date_out BETWEEN ? AND ? ''' database_cursor.execute(sql, (start_date, end_date)) How do I get the string that is parsed...

Python Selenium On Local HTML String

I am trying to run Selenium on a local HTML string but can't seem to find any documentation on how to do so. I retrieve HTML source from an e-mail API, so Selenium won't be able to parse it directly. Is there anyway to alter the following so that it would read the HTML string below: Python Code for remote access: from selenium import webdriver ...

Python deque: difference from list

I'm reading the Python Documentation: I don't understand how a deque is different from a list. From the documentation: Returns a new deque object initialized left-to-right (using append()) with data from iterable. If iterable is not specified, the new deque is empty. Deques are a generalization of stacks and queues (the name is pronounce...

Python random sequence with seed

I'm doing this for a school project (so I can't use any advanced features) and I'm using Python 2.6.6. I have a list of numbers from 1 to 1000 and my seed will be, lets say, 448. How can I generate a random sequence with that seed so that the numbers in my list will be in a different index? And is it possible, knowing the seed, return the eleme...

Python: Split string by list of separators

In Python, I'd like to split a string using a list of separators. The separators could be either commas or semicolons. Whitespace should be removed unless it is in the middle of non-whitespace, non-separator characters, in which case it should be preserved. Test case 1: ABC,DEF123,GHI_JKL,MN OP Test case 2: ABC;DEF123;GHI_JKL;MN OP Test case 3:...

Python how to replace backslash with re.sub()

I have the following string mystr1 = 'mydirname' myfile = 'mydirname\myfilename' I'm trying to do this newstr = re.sub(mystr1 + "\","",myfile) How do I escape the backslash I'm trying to concatenate to mystr1? Solution to answer: You need a quadruple backslash: newstr = re.sub(mystr1 + "\\\\", "", myfile) Reason: Regex to match a sin...