This question already has answers here:
How to read a large file - line by line?
(11 answers)
Closed 4 years ago.
I am new in python and I am trying to print a list line by line.
fp = open(filepath) # Open file on read mode
lines = fp.read().split("\n") #Create a list with each line
print(lines) #Print the list
for line in lines:
print(line) #Print each line
fp.close()
But it's printing in one line.
The contents of the text file are
peat1,1,11345674565,04-11-2018
peat2,0,11345674565,05-11-2018
peat3,1,11345674565,06-11-2018
peat4,0,11345674565,07-11-2018
And it is printing as
peat1,1,11345674565,04-11-2018 peat2,0,11345674565,05-11-2018 peat3,1,11345674565,06-11-2018 peat4,0,11345674565,07-11-2018
The environment is -- Python 3.4 and running under Apache through cgi-bin
Any help is highly appreciated.
With large files, you are better off reading files line by line, like so:
with open('filename') as file:
for line in file:
print(line)
I suggest this approach with with, which handles closing the file pointer itself.
Related
This question already has answers here:
Is it possible to modify lines in a file in-place?
(5 answers)
Closed 2 years ago.
Consider a file with the following lines:
remove
keep
remove
Is it possible to remove the current line while iterating the file lines?
for word in file:
if word != "keep":
remove_line_from_file
In the end the file should just the line with word keep.
I know I could create a file with the remaining words but I was hoping to keep the same file.
Python has a nice library named fileinput which allows you to modify files inplace. You can print what you want to keep back into the file:
with fileinput.input(filename, inplace=True) as lines:
for line in lines:
if line == 'keep':
print(line,)
No, but you can extract all the contents of the file beforehand, modify the text, and then rewrite them back into the file:
with open('file.txt','r') as f:
lines = f.readlines()
with open('file.txt','w') as f:
f.write(''.join([ln for ln in lines if 'keep' in ln])) # Writes all the lines back to file.txt that has the word `keep` in them
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 3 years ago.
I'm trying to read a file (example below), line by line, backwards using Python.
abcd 23ad gh1 n d
gjds 23iu bsddfs ND31 NG
Note: I'm not trying to read the file from the end to the beginning, but I want to read each line starting from the end, i.e d for line 1, and NG for line 2.
I know that
with open (fileName) as f:
for line in f:
reads each line from left to right, I want to read it from right to left.
Try this:
with open(fileName, 'r') as f:
for line in f:
for item in line.split()[::-1]:
print(item)
If your file is not too big, you can read lines in reverse easily
with open(fileName) as f:
for line in reversed(f.readlines()):
# do something
Otherwise, I believe you'd have to use seed.
This question already has answers here:
How to read the entire file into a list in python?
(6 answers)
Closed 5 years ago.
I would like to save a file.txt line by line in variable (array or list)
.So if the file.txt is:
hi there
what's up
I want my code to save it line by line in the same variable making a table so I can access easily each line when I want using that variable. And what can I do if I wanna access only line 2? It is supposed I don't know how many lines the file.txt has.
Thank you very much!
Well it is super easy in Python:
text_file = open("filename.txt", "r")
# Splits the element by "\n"
lines = text_file.readlines()
text_file.close()
#Print List of Lines in your File
print(lines)
#Print Number of Lines in your File
print(len(lines))
#Access Second Line in your file
print(lines[1])# Since python is zero indexed
This question already has answers here:
How to delete a specific line in a file?
(17 answers)
Closed 6 years ago.
So I'm attempting to clean up a text file that I have (it is actually a region file which can be loaded into the astronomy fits viewer DS9). I would like to remove/delete the entirety of any line/(s) which contain the keyword "red" in them, for an example:
image;circle(2384.21957861,231.579450647,10.3410929712) # color = red text = {24}
Would anyone know how this could be accomplished within python?
Open an outfile (outfile.txt) for writing, and open your input file (textfile.txt), going line by line through the input file scanning for the keyword (red). If red is not in the line it writes it to the outfile.
with open('outfile.txt', 'w') as o:
with open('textfile.txt') as f:
for line in f.readlines():
if 'red' not in line:
o.write(line)
Make sure the files are within the same directory as the python script.
Based on this answer suggested by #algor, you coul try:
f = open("textfile.txt","r+")
d = f.readlines()
f.seek(0)
for line in d:
if 'red' not in line:
f.write(i)
f.truncate()
f.close()
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Reading huge data from files and calling them
I do not know how to search through a file, I have a file which has around 50 lines of data in this format (1.000 2.000 3.000) but I do not know how to do one line do a conversion(already have) then go to the next line do the same until it reaches the end, so basically do a process line by line until no more lines.
with open('filename') as f:
for line in f:
line = line.rstrip()
# do the conversion (that you already know how to do)
Here:
with open('filename') as f: opens the file (and automatically closes it at the end);
for line in f: reads every line of the file into line;
line = line.rstrip() removes any trailing whitespaces and the newline character from line.