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.
Related
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.
I am trying to get the code below to read the file raw.txt, split it by lines and save every individual line as a .txt file. I then want to append every text file to splits.zip, and delete them after appending so that the only thing remaining when the process is done is the splits.zip, which can then be moved elsewhere to be unzipped. With the current code, I get the following error:
Traceback (most recent call last): File "/Users/Simon/PycharmProjects/text-tools/file-splitter-txt.py",
line 13, in <module> at stonehenge summoning the all father. z.write(new_file)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 1123, in write st = os.stat(filename) TypeError: coercing to Unicode: need string or buffer,
file found
My code:
import zipfile
import os
z = zipfile.ZipFile("splits.zip", "w")
count = 0
with open('raw.txt','r') as infile:
for line in infile:
print line
count +=1
with open(str(count) + '.txt','w') as new_file:
new_file.write(str(line))
z.write(new_file)
os.remove(new_file)
You could simply use writestr to write a string directly into the zipFile. For example:
zf.writestr(str(count) + '.txt', str(line), compress_type=...)
Use the file name like below. write method expects the filename and remove expects path. But you have given the file (file_name)
z.write(str(count) + '.txt')
os.remove(str(count) + '.txt')
In python I'm seeing evidence that fp.readlines() is closing the file when I try to access the fp later in the program. Can you confirm this behavior, do I need to re-open the file again later if I also want to read from it again?
Is the file closed? is similar, but didn't answer all of my questions.
import sys
def lines(fp):
print str(len(fp.readlines()))
def main():
sent_file = open(sys.argv[1], "r")
lines(sent_file)
for line in sent_file:
print line
this returns:
20
Once you have read a file, the file pointer has been moved to the end and no more lines will be 'found' beyond that point.
Re-open the file or seek back to the start:
sent_file.seek(0)
Your file is not closed; a closed file raises an exception when you attempt to access it:
>>> fileobj = open('names.txt')
>>> fileobj.close()
>>> fileobj.read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
It doesn't close the file, but it does read the lines in it so they cannot be read again without reopening the file or setting the file pointer back to the beginning with fp.seek(0).
As evidence that it doesn't close the file, try changing the function to actually close the file:
def lines(fp):
print str(len(fp.readlines()))
fp.close()
You will get the error:
Traceback (most recent call last):
File "test5.py", line 16, in <module>
main()
File "test5.py", line 12, in main
for line in sent_file:
ValueError: I/O operation on closed file
It won't be closed, but the file will be at the end. If you want to read its contents a second time then consider using
f.seek(0)
You may want to use the with statement and context manager:
>>> with open('data.txt', 'w+') as my_file: # This will allways ensure
... my_file.write('TEST\n') # that the file is closed.
... my_file.seek(0)
... my_file.read()
...
'TEST'
If you use a normal call, remember to close it manually (in theory python closes file objects and garbage collect them as needed):
>>> my_file = open('data.txt', 'w+')
>>> my_file.write('TEST\n') # 'del my_file' should close it and garbage collect it
>>> my_file.seek(0)
>>> my_file.read()
'TEST'
>>> my_file.close() # Makes shure to flush buffers to disk
I am reading in a file and wonder if there's a way to read the next line in a for loop?
I am currently reading the file like this:
file = open(input,"r").read()
for line in file.splitlines():
line = doSomething()
So is there anyway I can retrieve the next line of the file in that for loop such that I can perform some operation in the doSomething() function?
Just loop over the open file:
infile = open(input,"r")
for line in infile:
line = doSomething(line, next(infile))
Because you now use the file as an iterator, you can call the next() function on the infile variable at any time to retrieve an extra line.
Two extra tips:
Don't call your variable file; it masks the built-in file type object in python. I named it infile instead.
You can use the open file as a context manager with the with statement. It'll close the file for you automatically when done:
with open(input,"r") as infile:
for line in infile:
line = doSomething(line, next(infile))
file = open(input,"r").read()
lines = file.read().splitlines()
for i in range(len(lines)):
line = lines[i]
next_line = lines[i+1]
I think that you mean that if you are in line n, you want to be able to access line n+1.
The simplest way to do that is to replace
for line in file.splitlines():
with
lines = file.readlines()
for i in xrange(len(lines)):
then you can get the current line with lines[i] and the next line with lines[i+1]
the more pythonic way is to use enumerate
lines = file.readlines()
for index, line in enumerate(lines):
now you have the current line in "line" like normal, but you also have the index if you want to find a different line relative to it.
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!