Trouble using dicts parsed by AST - python

I have looked at all the other posts here asking the same question, but I still cannot figure out why I keep getting this Traceback. The strange thing is that the program works as intended but always brings up this Traceback at the end:
Traceback (most recent call last):
File "/home/me/Python/NextLogAudit.py", line 5, in <module>
i = ast.literal_eval(i)
File "/usr/lib/python3.7/ast.py", line 46, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
File "/usr/lib/python3.7/ast.py", line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 1
^
SyntaxError: unexpected EOF while parsing
I am trying to figure out how to get rid of this error so the program can exit cleanly. Also, it won't allow me to put 2 conditions in my if statement regarding the dict, so I had to nest the second condition in the first if. I'm pretty sure it has something to do with how AST is parsing the dict but cannot figure it out. The file I am opening is a list of dictionaries in string format:
with open('/home/me/Python/logtest') as f:
for i in f.readlines():
i = ast.literal_eval(i)
if re.search("Preview accessed.+", i["message"]):
if i["user"] == "user1":
name = re.search('(?<=Preview accessed: \").+(?=\.)', \
i["message"])
print("{} viewed {} on {}".format(i["user"], \
name.group().replace('\\',''),
i["time"].replace('+00:00','')))
else:
print("Nothing")

You need to guard against empty lines - there is one after all your data:
with open('/home/me/Python/logtest') as f:
for i in f.readlines():
if not i.strip(): # do nothing for empty lines
continue
i = ast.literal_eval(i)
# ... rest of your code ...
Else it reads something that is not an dictionary after evaling it and you index into when using
i["message"]
which does not work.

Related

python execute formatted string, SyntaxError: unexpected character after line continuation character

The thing sounded to be pretty simple
I've string, written into file, it's the formatted string as a string
I mean, file looks like this:
f\"Welcome {member.name}\\nNice to see you\"
So the code sees it like this:
'f"Welcome {member.name}\\nNice to see you"'
Then I've just placed it into exec() branch, so it's like this:
with open('welcome.txt', 'r') as file:
data = file.read()
for member in members:
temp = exec(data)
print(temp)
Though the only thing I get back is this error:
Ignoring exception in on_member_join
Traceback (most recent call last):
File "C:\Users\grzes\PycharmProjects\Mash_The_CharacterCreator\venv\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:/Users/grzes/PycharmProjects/Mash_The_CharacterCreator/bot.py", line 335, in on_member_join
temp = exec(serverConfig[str(member.guild.id)]['Welcome_Msg'])
File "<string>", line 1
f\"Welcome {member.name}\\nNice to see you\"
^
SyntaxError: unexpected character after line continuation character
Does anyone knows why does this happen, and how to solve this?
i,ve got it like:
print(f"Welcome {member.name} \nNice to see you")
with open('welcome.txt', 'r') as file:
data = file.read()
for member in members:
exec(data)
print(temp) will give you a None, so i deleted it,
used print on the formatting line instead
It should be
print(f'Welcome {member.name}\nNice to see you')
You shouldn't need all of those backslashes, just the one for a newline

(Python 3.x)Syntax Error EOF while parsing when reading a line from a file.

(Python 3.x)So I keep getting a syntax error when reading a file. I've had this problem for a while now. I've been working on other parts of the program so far(not shown), but can't solve this syntax error. I am very confused.
I feel guilty posting about a syntax error but I am out of ideas.
Here is the error: Syntax Error: unexpected EOF while parsing: , line 0, pos 0 # line 8
Code:
def main():
filename = 'p4input.txt'
infile = open(filename, "r")
command = 0
while command != 3 and command < 3:
command = eval(infile.readline()) #Problem here
convert = eval(infile.readline())
print(command)
print(convert)
print("done")
main()
The input file (p4input.txt)
Has the following data:
2
534
1
1101
Complete traceback:
Traceback (most recent call last):
File "C:/Users/Ambrin/Desktop/CS 115/TESTER.py", line 16, in <module>
main()
File "C:/Users/Ambrin/Desktop/CS 115/TESTER.py", line 8, in <module>
command = eval(infile.readline())
File "<string>", line 0, in ?
Syntax Error: unexpected EOF while parsing: <string>, line 0, pos 0
This is happening because when you get to the end of the file, readline() returns an empty string, so you're doing eval(''). You need to check for an empty string and break.
As pointed out in a comment above, you probably shouldn't be using eval. If all your inputs are expected to be integers, you can just use int() instead. You'll still need to check for '' though.

Processing a for loop with an error

I have a for loop which has an error in it.
try:
for line in text:
'do stuff'
except:
pass
When the error occurs python just exits the for loop. I can't get python to ignore the error and keep iterating through the loop. Incidentally, it is a text file and I am looping through the lines. I should also point out that the error does not occur in the do stuff part, it literally occurs in the for loop. I also frankly don't understand why the error is being thrown since the line is just like any other line. I tried deleting the line to see if it was just a one time thing but the next line has an error in it too which leads me to believe that I cannot just delete bad lines. The name of the error is unicodedecodeerror
Here's the text:
https://drive.google.com/file/d/0B9zzW6-3m2qGVFRTbzlXMS0tVUU/view?usp=sharing
I'm trying to make a list of all the words that follow the word 'abstract'
Here's the actual code
with open(full_path_of_old_file) as old:
for i, line in enumerate(old):
if "abstract," not in line:
b = line.find('abstract')
line2 = line[b+9:]
list1 = line2.split()
list1[0] = list1[0].replace(",","")
list1[0] = list1[0].replace(".", "")
try:
if list1[0][-1] == "s":
list1[0] = list1[0][:-1]
except:
pass
objects_of_abstract.append(list1[0])
Here's the full traceback
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1596, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1023, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/kylefoley/PycharmProjects/inference_engine2/inference2/Proofs/z_natural_language.py", line 25, in <module>
for i, line in enumerate(old):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 764: invalid continuation byte
We've got an error while stopping in post-mortem: <class 'KeyboardInterrupt'>
Your problem is your file encoding. Change it to latin-1 like this:
objects_of_abstract = list()
with open('abstract.txt', encoding="latin-1") as old:
for i, line in enumerate(old):
if "abstract," not in line:
b = line.find('abstract')
line2 = line[b+9:]
list1 = line2.split()
list1[0] = list1[0].replace(",","")
list1[0] = list1[0].replace(".", "")
try:
if list1[0][-1] == "s":
list1[0] = list1[0][:-1]
except:
pass
objects_of_abstract.append(list1[0])
Somewhere in your code you open the file. There are two parameters in the open function that pertain to your problem:
encoding=None: encoding is the name of the encoding used to decode or encode the file. This should only be used in text mode. The default encoding is platform dependent (whatever locale.getpreferredencoding() returns), but any encoding supported by Python can be used. See the codecs module for the list of supported encodings.
errors=None: errors is an optional string that specifies how encoding and decoding errors are to be handled–this cannot be used in binary mode. A variety of standard error handlers are available, though any error handling name that has been registered with codecs.register_error() is also valid.
[there is more; see the standard library docs]
Since you don't show how you open the file, I can't tell you specifically what is wrong. But the error message indicates that it is an encoding error, and you should start by looking into that.
A possible answer, which should bypass your problem to the other answers, is to use the suppress function from the contextlib
module in the standard library:
from contextlib import suppress
with suppress(UnicodeDecodeError): #might have to some some work here to get the error right
for i, line in enumerate(old):
.....
as it says in the docs though, better to try and fix your problem, rather than silently ignore it, if possible.
This should help with the first question (keeping the loop going):
In a python try...except block, it will try to run all the code in the try block, and if an error is thrown, it will stop and move to the except
In your case, you could move the for outside the try, so that if an error occurs it will be handled in the except and then continue on to the next iteration:
for line in text:
try:
'do stuff'
except: # if 'do stuff' throws an error, we just go to the next iteration
pass
You have to keep try/except block inside for loop
for line in text:
try:
# your Operation
except:
pass

How to skip reading first line from file when using fileinput method

I am doing this to read the file:
import fileinput
for line in fileinput.input('/home/manish/java.txt'):
if not fileinput.isfirstline():
... data = proces_line(line);
... output(data)
It is throwing error as proces_line is not defined.
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
NameError: name 'proces_line' is not defined
I have to read the data line by line and store in list, each line being separate element of list.
You can skip the first line as follows:
import fileinput
def output(line):
print(line)
fi = fileinput.input('/home/manish/java.txt')
next(fi) # skip first line
for line in fi:
output(line)
This avoids you having to test for a first line each time in the for loop.
To store each of the lines into a list, you could do the following:
import fileinput
fi = fileinput.input('/home/manish/java.txt')
next(fi) # skip first line
output = list(fi)
fi.close()
print(output)
You can try with this:
fname = '/home/manish/java.txt'
with open(fname) as f:
content = f.readlines()
content is of type list. You can ignore content[0] and loop through with the rest to fetch the required data.
You are looking for the "readline ()" fuction. Pulls in the next line from the file and truncated the newline Python documentation for File Input
Usage
For each in openFile:
List += openFile.readline ()
In addition, you are trying to use a function that does not exist. As well as being miss spelled.

Python: Deleting specific strings from file

I am reposting after changing a few things with my earlier post. thanks to all who gave suggestions earlier. I still have problems with it.
I have a data file (un-structed messy file) from which I have to scrub specific list of strings (delete strings).
Here is what I am doing but with no result:
infile = r"messy_data_file.txt"
outfile = r"cleaned_file.txt"
delete_list = ["firstname1 lastname1","firstname2 lastname2"....,"firstnamen lastnamen"]
fin = open(infile,"")
fout = open(outfile,"w+")
for line in fin:
for word in delete_list:
line = line.replace(word, "")
fout.write(line)
fin.close()
fout.close()
When I execute the file, I get the following error:
NameError: name 'word' is not defined
I'm unable to replicate your error; the error I get with your code is the empty mode string - either put "r" or delete it, read is the default.
Traceback (most recent call last):
File "test.py", line 6, in <module>
fin = open(infile, "")
ValueError: empty mode string
Otherwise, seems fine!

Categories