How can I convert this so it could execute?
Traceback (most recent call last):
File "C:\Users\Shady\Desktop\tet.py", line 14, in <module>
exec test
File "<string>", line 1
print "hello world"
^
IndentationError: unexpected indent
source:
test = ''.join(clientIp.split("test6")[1:])
You might need to use lstrip() on the string to get rid of any leading whitespace before passing it to exec.
Related
Why these below statements are giving error
>>> exec("x={}".format('b'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'b' is not defined
I need the result to be
x='b'
You should provide another pair of quotes
>>> exec("x={}".format("'b'"))
>>> x
'b'
Why?
When you write
exec("x={}".format('b'))
you are trying to write
x=b
obviously python doesn't know what b is unless you have defined it before.
Where as when you write
exec("x={}".format("'b'"))
It is same as
x='b'
If I want to see the str.replace() function: help(str.replace)
the result is:
Help on method_descriptor:
replace(...)
S.replace(old, new[, count]) -> str
Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
(END)
but how use help file.read or readlines?
for example, help(file.read) and help(read) are both errors:
>>> help(file)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'file' is not defined
>>> help(file.read)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'file' is not defined
>>> help(read)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'read' is not defined
How can I use help see file functions?
The file type has been removed from Python 3. Look at the io module instead:
>>> import io
>>> help(io.TextIOBase.read)
Help on method_descriptor:
read(...)
Read at most n characters from stream.
Read from underlying buffer until we have n characters or we hit EOF.
If n is negative or omitted, read until EOF.
Lets say I have a string
s="""
print 'hi'
print 'hi'
print 3333/0
"""
Is there a module or way that can help me check the syntax of this string?
I would like the output to be like:
Line 2, indentation
Line 3, Division by Zero
I have heard of pyFlakes, pyChecker and pyLint but those check a file, not a string.
The compile() function will tell you about compile-time errors:
try:
compile(s, "bogusfile.py", "exec")
except Exception as e:
print "Problem: %s" % e
Keep in mind though: one error will prevent the others from being reported, and some of your errors (ZeroDivision) are a run-time error, not something the compiler detects.
s="""
print 'hi'
print 'hi'
print 3333/0
"""
eval(s)
output:
Traceback (most recent call last):
File "prog.py", line 7, in <module>
eval(s)
File "<string>", line 3
print 'hi'
^
SyntaxError: invalid syntax
Hello is use eclipse and pydev, I was wondering why my sample code won't work trying to use the while function.
print("Welcome to the annoying program")
response = ""
while response != "Because.":
response = input("why\n")
print("Oh,ok")
the output is the following:
Welcome to the annoying program
why
Because.
Traceback (most recent call last):
File "/Users/calebmatthias/Document/workspace/de.vogella.python.first/simpprogram.py", l ine 9, in <module>
response = input("why\n")
File "/Users/calebmatthias/Desktop/eclipse 2/plugins/org.python.pydev_2.2.3.2011100616/PySrc/pydev_sitecustomize/sitecustomize.py", line 210, in input
return eval(raw_input(prompt))
File "<string>", line 1
Because.
^
SyntaxError: unexpected EOF while parsing
The input function in 2.x evaluates the input as Python code! Use the raw_input function instead.
With input, your text "Because." is being evaluated, and it's a syntax error because the dot isn't followed by anything.
How do I validate an expression/infix in python? Is it possible?
For example:
a-d*9
5-(a*0.3-d+(0.4-e))/k*5
(a-d*9)/(k-y-4.3*e)+(t-7*c)
If you want Python-style expressions, you can use the parser in the ast module and check for SyntaxError:
>>> ast.parse('5-(a*0.3-d+(0.4-e))/k*5')
<_ast.Module object at 0x7fc7bdd9e790>
>>> ast.parse('5-(a*0.3-d+(0.4-e))/k*')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/ast.py", line 37, in parse
return compile(expr, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 1
5-(a*0.3-d+(0.4-e))/k*
^
SyntaxError: unexpected EOF while parsing
Though that might parse much more than you actually need:
>>> ast.parse('def spam(): return "ham"')
<_ast.Module object at 0x7fc7bdd9e790>
so you might want to inspect the returned parse tree carefully.