appending a semicolon to end of a line - python

I was intersted to know if there is a easy way to append a semicolon to the end of each line. I tried but always it prints the semicolon in the next line.
rom_data.txt
0123
1
253
3
my_script.py
input_file = open("rom_data.txt", "r")
for line in input_file:
final= line+';'
print final
expected output
0123;
1;
253;
3;
output obtained
0123
;
1
;
253
;
3
;
Could anybody tell me where am i going wrong

so your text file consists of lines. if you ask a viewer to show non-printable characters, it will show something like
0123\n
1\n
253\n
3\n
(or some other symbol marking the line break)
for line in input_file: # so here "line" is "0123\n"
final= line+';' # here you append a semicolon, so it becomes "0123\n;"
print final # print adds another line break, so the output is "0123\n;\n"
a common solution would be to strip the line breaks first thing in the loop:
for line in input_file:
line = line.strip() # here
final= line+';'
print final

The solution of #Pavel points out the problem, but notice that there may be some problem.
That if you initial rom_data row begins or ends with some blank character, the strip() function will remove them all, which may not as expected.
For example:
rom_data.txt:
0123 \n
123 \n
456\n
may obtain output below:
0123\n
123\n
456\n
If you want to keep the blanks, you should only strip the last character:
for line in input_file:
print(line[:-1]) # use this way to strip only the '\n' character at last
This may be more exact.

The problem is that there is a newline character present in line. You will need to strip the newline character using rstrip. Refer below code:
input_file = open("rom_data.txt", "r")
for line in input_file:
final= line.rstrip('\n') + ';'
print final

Related

I'm trying to solve this Python exercise but I have no idea of how to do it: get first character of a line from a file + length of the line

I am learning Python on an app called SoloLearn, got to solve this exercise and I cannot see the solution or see the comments, I don't need to solve it to continue but I'd like to know how to do it.
Book Titles: You have been asked to make a special book categorization program, which assigns each book a special code based on its title.
The code is equal to the first letter of the book, followed by the number of characters in the title.
For example, for the book "Harry Potter", the code would be: H12, as it contains 12 characters (including the space).
You are provided a books.txt file, which includes the book titles, each one written on a separate line.
Read the title one by one and output the code for each book on a separate line.
For example, if the books.txt file contains:
Some book
Another book
Your program should output:
S9
A12
Recall the readlines() method, which returns a list containing the lines of the file.
Also, remember that all lines, except the last one, contain a \n at the end, which should not be included in the character count.
I tried:
file = open("books.txt","r")
for line in file:
for i in range(len(file.readlines())):
title = line[0]+str(len(line)-1)
print(titulo)
title = line[0]+str(len(line)-1)
print(title)
file.close
I also tried with range() and readlines() but I don't know how to solve it
This uses readlines():
with open('books.txt') as f: # Open file
for line in f.readlines(): # Iterate through lines
if line[-1] == '\n': # Check if there is '\n' at end of line
line = line[:-1] # If there is, ignore it
print(line[0], len(line), sep='') # Output first character and length
But I think splitlines() is easier, as it doesn't have the trailing '\n':
with open('books.txt') as f: # Open file
for line in f.read().splitlines(): # Iterate through lines
# No need to check for trailing '\n'
print(line[0], len(line), sep='') # Output first character and length
You can use "with" to handle file oppening and closing.
Use rstrip to get rid of '\n'.
with open('books.txt') as f:
lines = file.readlines()
for line in lines:
print(line[0] + str(len(line.rstrip())))
This is the same:
file = open('books.txt')
lines = file.readlines()
for line in lines:
print(line[0] + str(len(line.rstrip())))
file.close()

How do I combine lines in a text file in a specific order?

I'm trying to transform the text in a file according the following rule: for each line, if the line does not begin with "https", add that word to the beginning of subsequent lines until you hit another line with a non-https word.
For example, given this file:
Fruit
https://www.apple.com//
https://www.banana.com//
Vegetable
https://www.cucumber.com//
https://www.lettuce.com//
I want
Fruit-https://www.apple.com//
Fruit-https://www.banana.com//
Vegetable-https://www.cucumber.com//
Vegetable-https://www.lettuce.com//
Here is my attempt:
one = open("links.txt", "r")
for two in one.readlines():
if "https" not in two:
sitex = two
else:
print (sitex + "-" +two)
Here is the output of that program, using the above sample input file:
Fruit
-https://www.apple.com//
Fruit
-https://www.banana.com//
Vegetable
-https://www.cucumber.com//
Vegetable
-https://www.lettuce.com//
What is wrong with my code?
To fix that we need to implement rstrip() method to sitex to remove the new line character at the end of the string. (credit to BrokenBenchmark)
second, the print command by default newlines everytime it's called, so we must add the end="" parameter to fix this.
So your code should look like this
one = open("links.txt", "r")
for two in one.readlines():
if "https" not in two:
sitex = two.rstrip()
else:
print (sitex + "-" +two,end="")
one.close()
Also always close the file when you are done.
Lines in your file end on "\n" - the newline character.
You can remove whitespaces (includes "\n") from a string using strip() (both ends) or rstrip() / lstrip() (remove at one end).
print() adds a "\n" at its end by default, you can omit this using
print("something", end=" ")
print("more) # ==> 'something more' in one line
Fix for your code:
# use a context handler for better file handling
with open("data.txt","w") as f:
f.write("""Fruit
https://www.apple.com//
https://www.banana.com//
Vegetable
https://www.cucumber.com//
https://www.lettuce.com//
""")
with open("data.txt") as f:
what = ""
# iterate file line by line instead of reading all at once
for line in f:
# remove whitespace from current line, including \n
# front AND back - you could use rstring here as well
line = line.strip()
# only do something for non-empty lines (your file does not
# contain empty lines, but the last line may be empty
if line:
# easier to understand condition without negation
if line.startswith("http"):
# printing adds a \n at the end
print(f"{what}-{line}") # line & what are stripped
else:
what = line
Output:
Fruit-https://www.apple.com//
Fruit-https://www.banana.com//
Vegetable-https://www.cucumber.com//
Vegetable-https://www.lettuce.com//
See:
str.lstrip([chars])
str.rstrip([chars])
str.strip([chars])
[chars] are optional - if not given, whitespaces are removed.
You need to strip the trailing newline from the line if it doesn't contain 'https':
sitex = two
should be
sitex = two.rstrip()
You need to do something similar for the else block as well, as ShadowRanger points out:
print (sitex + "-" +two)
should be
print (sitex + "-" + two.rstrip())

Appending string to a line read from file places appended string on next line

I have a .txt file like the following:
abc
def
ghi
Now, I want to add some string behind each row directly. However, my output is:
abc
---testdef
---testghi---test
My code is as follows:
file_read = open("test.txt", "r")
lines = file_read.readlines()
file_read.close()
new_file = open("res.txt", "w")
for line in lines:
new_file.write(line + "---test") # I tried to add "\r" in the middle of them, but this didn't work.
new_file.close()
You need to strip the new line using .rstrip():
for line in lines:
new_file.write(f"{line.rstrip()}---test\n")
Then, res.txt contains:
abc---test
def---test
ghi---test
What I understood is, you want to add a string behind a string, for example "abcd" should be changed into "---testabcd".
So the mistake you made is in new_file.write(line + "---test"), if you want add string1 before a string2, then you have to specify string1 first then string2.
So change it tonew_file.write("---test" + line)
Tip: Instead of using '+' operator, use f strings or .format.
f"---test{line}" this is f string.
"Hello {friends}".format(friends="myname")
For use of '\r':
Whenever you will use this special escape character \r, the rest of the content after the \r will come at the front of your line and will keep replacing your characters one by one until it takes all the contents left after the \r in that string.
print('Python is fun')
Output: Python is fun
Now see what happens if I use a carriage return here
print('Python is fun\r123456')
Output: 123456 is fun
So basically, it just replaces indexes of string to character after \r.

Python - delete blank lines of text at the end of the file

I am writing a script that modifies any text files. It replaces white space lines with blank lines. It erases the blank lines at the end of the file. The image shows the output I want.
I am able to get very close to the desired output. The problem is that I cannot get rid of the last blank line. I think this has something to do with the last line. e.g ' the lines below me should be gone actually looks like this ' the lines below me should be gone\n' It looks like new lines are created on the previous line. e.g if line 4 has \n than line 5 will actually be the blank line not line 4.
I should note that I can't use rstrip or strip
My code so far.
def clean_file(filename):
# function to check if the line can be deleted
def is_all_whitespace(line):
for char in line:
if char != ' ' and char != '\n':
return False
return True
# generates the new lines
with open(filename, 'r') as file:
file_out = []
for line in file:
if is_all_whitespace(line):
line = '\n'
file_out.append(line)
# removes whitespaces at the end of file
while file_out[-1] == '\n': # while the last item in lst is blank
file_out.pop(-1) # removes last element
# writes the new the output to file
with open(filename, 'w') as file:
file.write(''.join(file_out))
clean_file('test.txt')
The \n essentially means "create another line"
So when you've removed all the lines that are \n, there's still the preceding line
the lines below me should be gone\n
Which again means "create another line", beyond the ones you've already removed
Since you say you can't use rstrip, you could end the loop with
file_out[-1] = file_out[-1].strip('\n')
to remove \n from the last element. Because \n can't exist anywhere else in a line, rstrip and strip will have the same effect
Or without any strip or endswith:
if file_out[-1][-1] == '\n':
file_out[-1] = file_out[-1][:-1]
Note that \n is a single character, ordinal 0x0a as hex, not two characters \ and n, ordinals 0x5c and 0x6e. That is why we use -1 and not -2

\n appending at the end of each line

I am writing lines one by one to an external files. Each line has 9 columns separated by Tab delimiter. If i split each line in that file and output last column, i can see \n being appended to the end of the 9 column. My code is:
#!/usr/bin/python
with open("temp", "r") as f:
for lines in f:
hashes = lines.split("\t")
print hashes[8]
The last column values are integers, either 1 or 2. When i run this program, the output i get is,
['1\n']
['2\n']
I should only get 1 or 2. Why is '\n' being appended here?
I tried the following check to remove the problem.
with open("temp", "r") as f:
for lines in f:
if lines != '\n':
hashes = lines.split("\t")
print hashes[8]
This too is not working. I tried if lines != ' '. How can i make this go away? Thanks in advance.
Try using strip on the lines to remove the \n (the new line character). strip removes the leading and trailing whitespace characters.
with open("temp", "r") as f:
for lines in f.readlines():
if lines.strip():
hashes = lines.split("\t")
print hashes[8]
\n is the newline character, it is how the computer knows to display the data on the next line. If you modify the last item in the array hashes[-1] to remove the last character, then that should be fine.
Depending on the platform, your line ending may be more than just one character. Dos/Windows uses "\r\n" for example.
def clean(file_handle):
for line in file_handle:
yield line.rstrip()
with open('temp', 'r') as f:
for line in clean(f):
hashes = line.split('\t')
print hashes[-1]
I prefer rstrip() for times when I want to preserve leading whitespace. That and using generator functions to clean up my input.
Because each line has 9 columns, the 8th index (which is the 9th object) has a line break, since the next line starts. Just take that away:
print hashes[8][:-1]

Categories