This is the code (sorry if I did it wrong I have no idea how to do that):
with open( 'output', 'r' ) as f:
print("this is line one: " + f.readline(1))
And this is what it prints:
this is line one:
I have checked the file so many times the one says "0". I've checked it in vscode. I've checked it by just opening it in the folder it definitely says 0, but it is just not working and yes it's on line 1.
I've looked all over google and stack overflow and nothing is at all helpful when it comes to my question.
Just to add explanation for the answer above, from python docs of readline
f.readline() reads a single line from the file; a newline character
(\n) is left at the end of the string, and is only omitted on the last
line of the file if the file doesn’t end in a newline. This makes the
return value unambiguous; if f.readline() returns an empty string, the
end of the file has been reached.
where its argument is similar to the size argument of the read function:
size is an optional numeric argument. When size is omitted or
negative, the entire contents of the file will be read and returned
Meaning: when you passed 1 the the readline function you actually read 1 byte from the line, instead of reading the entire line. when passing no arguments, the entire line is returned as a string up to the newline character \n
with open('output', 'r') as f:
print("this is line one: " + f.readline(1))
readline does not take an argument, but readlines takes a size (readlines(size:int) argument.
Though in your case you want to read one line, so readline() should do fine.
with open('output', 'r') as f:
print("this is line one: " + f.readline())
The syntax file.readline(size) read from the file a string of that size. If no argument is specified the readline() method will return the entire line from the given file.
So your function should be:
with open( 'output', 'r' ) as f:
print("this is line one: " + f.readline())
If you want to read all the lines in the file you can modify the function to this:
with open( 'output', 'r' ) as f:
for count, line in enumerate(f):
print("Line {}: {}".format(count, line))
Related
I am a beginner in Python and been playing around with what I know so far and came across the below problem. Can someone kindly help me out to understand why this occurs?
suppose I have a text named 'test.txt' file that contains the following,
This is the first line
This is the second line
This is the third line
I print every line in this text file by doing as below,
with open('test.txt', 'r') as f:
for line in f:
print(line)
However though, the output we get is,
This is the first line
This is the second line
This is the third line
As shown above we get an empty line printed due to each line in the text file containing a '\n' at the end of the each line.
To get rid of the empty line that is printed above, I know we could do something like,
with open('test.txt', 'r') as f:
for line in f:
print(line, end='')
This gives us the following output,
This is the first line
This is the second line
This is the third line
What I do not understand is, how have we been able to get rid of the newline character just by adding an empty string to the end of each line?
Note that '\n' is the default parameter given to end.
This is from the official python docs:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False).
You can check the docs here: https://docs.python.org/3/library/functions.html#print
The print function in python has a default value of \n for the parameter end, when you override that value by empty string '' => print(line, end='') you remove the new line behavior.
I have a file with content:
0x11111111
0x22222222
0x33333333
0x44444444
And I'm reading it line by line using:
f = open('test1', 'r')
print "Register CIP_REGS_CONTROL value:"
for i in range(4):
content = f.read(11)
f.seek(11, 1)
print content
Note that there're 11 bytes each line due to the '\n' char at the end. But the output is:
0x11111111
0x33333333
There's an empty line after the 1st and 3rd line, I don't know why it's like that. If I delete the '\n' in each line, and change the size of reading and seeking to 10, I got:
0x11111111
0x33333333
2 lines are also missing. Anybody can help? Thanks in advance.
Remove your seek call. Each call is skipping the next 11 bytes. That is read also moves the current position.
Two things:
You don't need to seek after the read. Your position in the file will already be at the next character after the call the read.
When you call print, it will add append a newline (\n) to your output.
The simplest (and safest - it ensures your file gets closed properly) way would be to use a with construct, and readline()
print "Register CIP_REGS_CONTROL value:"
with open('test1', 'r') as f:
for i in range(4):
print f.readline().strip()
strip() when called with no arguments removes all whitespace (which includes \n) from the beginning and end of a string.
The things I've googled haven't worked, so I'm turning to experts!
I have some text in a tab-delimited text file that has some sort of carriage return in it (when I open it in Notepad++ and use "show all characters", I see [CR][LF] at the end of the line). I need to remove this carriage return (or whatever it is), but I can't seem to figure it out. Here's a snippet of the text file showing a line with the carriage return:
firstcolumn secondcolumn third fourth fifth sixth seventh
moreoftheseventh 8th 9th 10th 11th 12th 13th
Here's the code I'm trying to use to replace it, but it's not finding the return:
with open(infile, "r") as f:
for line in f:
if "\n" in line:
line = line.replace("\n", " ")
My script just doesn't find the carriage return. Am I doing something wrong or making an incorrect assumption about this carriage return? I could just remove it manually in a text editor, but there are about 5000 records in the text file that may also contain this issue.
Further information:
The goal here is select two columns from the text file, so I split on \t characters and refer to the values as parts of an array. It works on any line without the returns, but fails on the lines with the returns because, for example, there is no element 9 in those lines.
vals = line.split("\t")
print(vals[0] + " " + vals[9])
So, for the line of text above, this code fails because there is no index 9 in that particular array. For lines of text that don't have the [CR][LF], it works as expected.
Depending on the type of file (and the OS it comes from, etc), your carriage return might be '\r', '\n', or '\r'\n'. The best way to get rid of them regardless of which one they are is to use line.rstrip().
with open(infile, "r") as f:
for line in f:
line = line.rstrip() # strip out all tailing whitespace
If you want to get rid of ONLY the carriage returns and not any extra whitespaces that might be at the end, you can supply the optional argument to rstrip:
with open(infile, "r") as f:
for line in f:
line = line.rstrip('\r\n') # strip out all tailing whitespace
Hope this helps
Here's how to remove carriage returns without using a temporary file:
with open(file_name, 'r') as file:
content = file.read()
with open(file_name, 'w', newline='\n') as file:
file.write(content)
Python opens files in so-called universal newline mode, so newlines are always \n.
Python is usually built with universal newlines support; supplying 'U'
opens the file as a text file, but lines may be terminated by any of
the following: the Unix end-of-line convention '\n', the Macintosh
convention '\r', or the Windows convention '\r\n'. All of these
external representations are seen as '\n' by the Python program.
You iterate through file line-by-line. And you are replacing \n in the lines. But in fact there are no \n because lines are already separated by \n by iterator and each line contains no \n.
You can just read from file f.read(). And then replace \n in it.
with open(infile, "r") as f:
content = f.read()
content = content.replace('\n', ' ')
#do something with content
Technically, there is an answer!
with open(filetoread, "rb") as inf:
with open(filetowrite, "w") as fixed:
for line in inf:
fixed.write(line)
The b in open(filetoread, "rb") apparently opens the file in such a way that I can access those line breaks and remove them. This answer actually came from Stack Overflow user Kenneth Reitz off the site.
Thanks everyone!
I've created a code to do it and it works:
end1='C:\...\file1.txt'
end2='C:\...\file2.txt'
with open(end1, "rb") as inf:
with open(end2, "w") as fixed:
for line in inf:
line = line.replace("\n", "")
line = line.replace("\r", "")
fixed.write(line)
I usually use the following Python code to read lines from a file :
f = open('./my.csv', 'r')
for line in f:
print line
But how about if the file is line delimited by "\0" (not "\n") ? Is there a Python module that could handle this ?
Thanks for any advice.
If your file is small enough that you can read it all into memory you can use split:
for line in f.read().split('\0'):
print line
Otherwise you might want to try this recipe from the discussion about this feature request:
def fileLineIter(inputFile,
inputNewline="\n",
outputNewline=None,
readSize=8192):
"""Like the normal file iter but you can set what string indicates newline.
The newline string can be arbitrarily long; it need not be restricted to a
single character. You can also set the read size and control whether or not
the newline string is left on the end of the iterated lines. Setting
newline to '\0' is particularly good for use with an input file created with
something like "os.popen('find -print0')".
"""
if outputNewline is None: outputNewline = inputNewline
partialLine = ''
while True:
charsJustRead = inputFile.read(readSize)
if not charsJustRead: break
partialLine += charsJustRead
lines = partialLine.split(inputNewline)
partialLine = lines.pop()
for line in lines: yield line + outputNewline
if partialLine: yield partialLine
I also noticed your file has a "csv" extension. There is a CSV module built into Python (import csv). There is an attribute called Dialect.lineterminator however it is currently not implemented in the reader:
Dialect.lineterminator
The string used to terminate lines produced by the writer. It defaults to '\r\n'.
Note The reader is hard-coded to recognise either '\r' or '\n' as end-of-line, and ignores lineterminator. This behavior may change in the future.
I have modified Mark Byers's suggestion so that we could READLINE file with NUL delimited lines in Python. This approach reads a potentially large file line by line and should be more memory efficient. Here is the Python code (with comments) :
import sys
# Variables for "fileReadLine()"
inputFile = sys.stdin # The input file. Use "stdin" as an example for receiving data from pipe.
lines = [] # Extracted complete lines (delimited with "inputNewline").
partialLine = '' # Extracted last non-complete partial line.
inputNewline="\0" # Newline character(s) in input file.
outputNewline="\n" # Newline character(s) in output lines.
readSize=8192 # Size of read buffer.
# End - Variables for "fileReadLine()"
# This function reads NUL delimited lines sequentially and is memory efficient.
def fileReadLine():
"""Like the normal file readline but you can set what string indicates newline.
The newline string can be arbitrarily long; it need not be restricted to a
single character. You can also set the read size and control whether or not
the newline string is left on the end of the read lines. Setting
newline to '\0' is particularly good for use with an input file created with
something like "os.popen('find -print0')".
"""
# Declare that we want to use these related global variables.
global inputFile, partialLine, lines, inputNewline, outputNewline, readSize
if lines:
# If there is already extracted complete lines, pop 1st llne from lines and return that line + outputNewline.
line = lines.pop(0)
return line + outputNewline
# If there is NO already extracted complete lines, try to read more from input file.
while True: # Here "lines" must be an empty list.
charsJustRead = inputFile.read(readSize) # The read buffer size, "readSize", could be changed as you like.
if not charsJustRead:
# Have reached EOF.
if partialLine:
# If partialLine is not empty here, treat it as a complete line and copy and return it.
popedPartialLine = partialLine
partialLine = "" # partialLine is now copied for return, reset it to an empty string to indicate that there is no more partialLine to return in later "fileReadLine" attempt.
return popedPartialLine # This should be the last line of input file.
else:
# If reached EOF and partialLine is empty, then all the lines in input file must have been read. Return None to indicate this.
return None
partialLine += charsJustRead # If read buffer is not empty, add it to partialLine.
lines = partialLine.split(inputNewline) # Split partialLine to get some complete lines.
partialLine = lines.pop() # The last item of lines may not be a complete line, move it to partialLine.
if not lines:
# Empty "lines" means that we must NOT have finished read any complete line. So continue.
continue
else:
# We must have finished read at least 1 complete llne. So pop 1st llne from lines and return that line + outputNewline (exit while loop).
line = lines.pop(0)
return line + outputNewline
# As an example, read NUL delimited lines from "stdin" and print them out (using "\n" to delimit output lines).
while True:
line = fileReadLine()
if line is None: break
sys.stdout.write(line) # "write" does not include "\n".
sys.stdout.flush()
Hope it helps.
I know how to do this with a file, you just do file = file.open(f) f
file = open("file.txt")
for line in file.readlines():
if line.startswith("foo"):
print line
But now I'm reading the output of a process like this
log = os.popen("log.sh").read()
This outputs as a string, which can be used with print fine, but if I do a "for line in log" it splits each character, not line. And, being a string, there is no .readlines() attribute.
My end goal is to be able to "grep" for a revision number in the log and print the line (and above and below)
for line in log.splitlines():
# whatever
Note that you don't need to use f.readlines() for a file f.
for line in f:
# whatever
will work fine.
You have several options:
Option 1:
log = os.popen("log.sh").readlines()
This gives a list of string that you can process exactly like you do when reading a file.
Option 2:
log = os.popen("log.sh").read()
for line in log.splitlines(True):
...