Salutations, I am trying to write a function that prints data from a text file line by line. The output needs to have the number of the line followed by a colon and a space. I came up with the following code;
def print_numbered_lines(filename):
"""Function to print numbered lines from a list"""
data = open(filename)
line_number = 1
for line in data:
print(str(line_number)+": "+line, end=' ')
line_number += 1
The issue is when I run this function using test text files I created, the first line is not on the same indentation level as the rest of the lines in the output, ie. the outputs look kind of like
1: 9874234,12.5,23.0,50.0
2: 7840231,70,60,85.4
3: 3845913,55.5,60.5,80.0
4: 3849511,20,60,50
Where am I going wrong? Thanks
Replace the value of end argument with empty string instead of space. As end argument is a space, it's printing a space after every line. So latter lines have a space at the beginning of the line.
def print_numbered_lines(filename):
"""Function to print numbered lines from a list"""
data = open(filename)
line_number = 1
for line in data:
print(str(line_number) + ": " + line, end='')
line_number += 1
Another way you can do this, is strip the new lines and print without passing any value to end argument. This will remove the \n it has at the end of the line and a new line will be printed as end="\n" by default.
def print_numbered_lines(filename):
"""Function to print numbered lines from a list"""
data = open(filename)
line_number = 1
for line in data:
print(str(line_number) + ": " + line.strip("\n"))
line_number += 1
This has to do with your print statement.
print(str(line_number)+": "+line, end=' ')
You probably saw that when printing your lines there was an extra line between them and then you tried to work around this by using end=' '.
If you want to remove the 'empty' lines you should use line.strip(). This removes them.
Use this:
print(str(line_number)+": "+line.strip())
strip can also take an argument. This is from the documentation:
str.strip([chars])
Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:
Whats up with that?
The lines in your file are not separated into different lines by nothing. On linux a newline is represented by \n. Normal editors convert these by pushing the text down into a new line.
When reading a file Python separates lines on exactly these \n but doesn't throw them away. When printing they will be interpreted again and combined with the newline a print adds there will be one newline 'too much'.
The end parameter in your print statement simply changes what print will use after printing a line. Default is \n.
Check what it does when you use end=" !":
1: aaa
!2: bbb
!3: ccc
You can see the \n after 'aaa' causing a newline (which is part of the string) and after that print adds the contents of end. So it adds a !. The next line is printed in the same line because there is no other newline that would cause a line break before printing it.
You specified end argument as a space. So after first line each has this extra space.
line that your read from file looks somehting like this:
'9874234,12.5,23.0,50.0\n'
Look at the ending. Line translation happens is due to original line.
So to get what you want you just need to change end argument of print to empty string( not space)
Moreover, I advise you to change the implementation of the function and use enumerate for line numbering.
def print_numbered_lines(filename):
data = open(filename)
for i, line in enumerate(data):
print(str(i+1)+": "+line, end='')
Related
When I try to use indexing to get the last element of my line, it does not work( prints out a blank line). However, when I print the original line it works.
with open( newFile,"w") as f:
f.write("Model Number One" + "\n")
for l in lines:
if "ATOM" in l :
f.write(l[-1]) #Does NOT work, prints empty line
f.write(l) # Prints the whole linem when I only want the last element of the line
When index is used
When index is not used
The cause might be that the line has a new line character at the end. If the last element of the line is a "\n" since the file has multiple lines, then it will definitely print a blank line.
Each line is a string, and so each index is a single character, with the last one being a linebreak ('\n'). If you want to get whitespace-separated substrings out of that line, use strip to remove the linebreak, and split to split the resulting string on whitespace:
with open( newFile, "w") as f:
f.write("Model Number One" + "\n")
for l in lines:
elems = l.strip().split()
if "ATOM" in elems:
f.write(elems[-1] + "\n")
As Kengru mentioned, there could be a newline at the end. To test try f.write(l[-2]) instead of f.write(l[-1]).
Having a hard time understanding what is happening in this snippet of code. Particularly with the 2nd line of code.
for line in infile:
data = line.strip('\n').split(':')
user_dict[data[0]] = data[1]
The line sets the variable data equal to the string represented by the variable line with the new line character '\n' removed and then split anywhere a : occurs.
It parses a file having this structure:
a:52
b:hi
key:value
for line in infile: is a loop for each line in the file. Each line (except for the last maybe) ends with new-line symbol \n.
line.strip('\n') removes the new-line symbol.
.split(':') splits the string into strings there were separated by :. For example: "qwe:rty:uio".split(':') -> ["qwe", "rty", "uio"]
user_dict[data[0]] = data[1] obviously saves the data into the dicionary user_dict taking the first string as a key, and second one as a value.
For the file mentioned above this code creates the following dictionary:
{"a": "52", "b": "hi", "key": "value"}
line.strip('\n') is removing all the \n (new line) from the string and the split(':') it is going to split your string using :
as the delimeter into an array of strings.
Above code is storing the file into the dictionary. Content of the file is like below
key1:value1
key2:value2
.
.
.
key3:value3
Second line is stripping off the \n character from the line and then splitting the each line by : character. However you should try to understand and debug the code line by line
line.strip('\n') will remove all the newlines from the string.
and
split(':') will split your string using ':' into array of strings.
data = line.strip('\n').split(':')
There are two string functions in one line. You also can separate the calls. This should be the same:
my_line = line.strip('\n')
my_line1 = my_line.split(':')
line.strip --> removes the new line character at the end of a line
line.split(':') --> splits the values at colon character and return a list of each record
It is easier to understand with concrete values.
Your file look like this and you loop through each line.
Name: Paul
Age: 18
Gender: Male
At the end of each line you have a "new line" character which will remove line.strip('\n').
Then you split the values at ":"
You finally create a dictionary (line 3) where key is the left side and the value is the right side.
dict['Name'] = 'Paul'
dict['Age'] = '18'
Basically line.strip('\n') removes leading consecutive newlines and trailing consecutive newlines, but leaves embedded newlines alone. from line; and then split(':') separates anywhere ":" is.This is then stored as a list in the variable named data.
'''
This is single line.
This is second long line
... continue from previous line.
This third single line.
'''
I want to join lines which separated by ellipsis(...). This I want to do in Python. The long line is separated by new line (\n) and ellipsis (...). I am reading this file line by line and doing some operation on specific lines, but continue line ends with new line (\n) and next line starts with ellipsis (...). Because of this I am not able to get full line to do specific operation.
The lines I have took as example were from big file (lines more than 800). The python utility parse the files, search lines with specific keywords and replace some portion of the line with new syntax. This I want to do on multiple files.
Please advise me.
You can simply do:
delim = '...'
text = '''This is single line.
This is second long line
... continue from previous line.
This third single line.
'''
# here we're building a list containing each line
# we'll clean up the leading and trailing whitespace
# by mapping Python's `str.strip` method onto each
# line
# this gives us:
#
# ['This is single line.', 'This is second long line',
# '... continue from previous line.', 'This third single line.', '']
cleaned_lines = map(str.strip, text.split('\n'))
# next, we'll join our cleaned string on newlines, so we'll get back
# the original string without excess whitespace
# this gives us:
#
# This is single line.
# This is second long line
# ... continue from previous line.
# This third single line.
cleaned_str = '\n'.join(cleaned_lines)
# now, we'll split on our delimiter '...'
# this gives us:
#
# ['This is single line.\nThis is second long line\n',
# ' continue from previous line.\nThis third single line.\n']
split_str = cleaned_str.split(delim)
# lastly, we'll now strip off trailing whitespace (which includes)
# newlines. Then, we'll join our list together on an empty string
new_str = ''.join(map(str.rstrip, split_str))
print new_str
which outputs
This is single line.
This is second long line continue from previous line.
This third single line.
You can split the lines on line breaks, and then loop through and add the ellipses lines to the previous line, like so:
lines = lines.split('\n')
for i, line in enumerate(lines):
line = line.strip().lstrip()
if line.startswith('...') and i != 0:
lines[i - 1] = lines[i - 1].strip().lstrip() + line.replace('...', '')
del lines[i]
f = open("test.txt", 'r+')
print ("Name of the file: ", f.name)
str = f.read();
str = str.split(',')
print(str2)
f.close()
I need to read from a file and it gives the name of the class it has to make and the parameters it needs to pass.
Example:
rectangle,9.7,7.3
square,6
so I have to make a rectangle object and pass those 2 parameters. then write it to another file with the results. I am stuck chopping up the string.
I use the split function to get rid of the commas, and it seems it returns a list which I am saving into the str list, which is probably bad, I should change the name. However, my concern is that although it does take the comma out. It keeps the ,\n, new line character and concatenates it to the next line. So it splits it like this ['rectangle', '9.7', '7.3\nsquare', ...
how can I get rid of that.
Any suggestions would be welcomed. Should I read line by line instead of reading the whole thing?
Try calling strip() on each line to get rid of the newline character before splitting it.
Give this a try (EDIT - Annotated code with comments to make it easier to follow):
# Using "with open()" to open the file handle and have it automatically closed for your when the program exits.
with open("test.txt", 'r+') as f:
print "Name of the file: ", f.name
# Iterate over each line in the test.txt file
for line in f:
# Using strip() to remove newline characters and white space from each end of the line
# Using split(',') to create a list ("tokens") containing each segment of the line separated by commas
tokens = line.strip().split(',')
# Print out the very first element (position 0) in the tokens list, which should be the "class"
print "class: ", tokens[0]
# Print out all of the remaining elements in the tokens list, starting at the second element (i.e. position "1" since lists are "zero-based")
# This is using a "slice". "tokens[1:]" means return the contents of the tokens list starting at position 1 and continuing to the end
# "tokens[1:3]" Would mean give me all of the elements of the tokens list starting at position 1 and ending at position 3 (excluding position 3).
# Loop over the elements returned by the slice, assigning them one by one to the "argument" variable
for argument in tokens[1:]:
# Print out the argument
print "argument: ", argument
output:
Name of the file: test.txt
class: rectangle
argument: 9.7
argument: 7.3
class: square
argument: 6
More information on slice: http://pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/
I want to add some letters to the beginning and end of each line using python.
I found various methods of doing this, however, whichever method I use the letters I want to add to then end are always added to the beginning.
input = open("input_file",'r')
output = open("output_file",'w')
for line in input:
newline = "A" + line + "B"
output.write(newline)
input.close()
output.close()
I have used varios methods I found here. With each one of them both letters are added to the front.
inserting characters at the start and end of a string
''.join(('L','yourstring','LL'))
or
yourstring = "L%sLL" % yourstring
or
yourstring = "L{0}LL".format(yourstring)
I'm clearly missing something here. What can I do?
When reading lines from a file, python leaves the \n on the end. You could .rstrip it off however.
yourstring = 'L{0}LL\n'.format(yourstring.rstrip('\n'))