This question already has answers here:
How can I remove a trailing newline?
(27 answers)
Closed 8 years ago.
I had a list that read from a text file into an array but now they all have "\n" on the end. Obviously you dont see it when you print it because it just takes a new line. I want to remove it because it is causing me some hassle.
database = open("database.txt", "r")
databaselist = database.readlines()
thats the code i used to read from the file. I am a total noob so please dont use crazy technical talk otherwise it will go straight over my head
"string with or without newline\n".rstrip('\n')
Using rstrip with \n avoids any unwanted side-effect except that it will remove multiple \n at the end, if present.
Otherwise, you need to use this less elegant function:
def rstrip1(s, c):
return s[:-1] if s[-1]==c else s
Use str.rstrip to remove the newline character at the end of each line:
databaselist = [line.rstrip("\n") for line in database.readlines()]
However, I recommend that you make three more changes to your code to improve efficiency:
Remove the call to readlines. Iterating over a file object yields its lines one at a time.
Remove the "r" argument to open since the function defaults to read-mode. This will not improve the speed of your code, but it will make it less redundant.
Most importantly, use a with-statement to open the file. This will ensure that it is closed automatically when you are done.
In all, the new code will look like this:
with open("database.txt") as database:
databaselist = [line.rstrip("\n") for line in database]
Related
This question already has answers here:
String comparison doesn't seem to work for lines read from a file
(2 answers)
Closed 1 year ago.
So I have a text file called Subject.txt with the contents "Fiction" (no quotations) inside it. One line, nothing else.
Using:
file = open("Subject.txt", "r")
Subject = (file.read())
file.close()
This writes the contents to a variable called Subject, which seems verified when I used
print(Subject)
and it returns Fiction as the response.
But this code...
if Subject == "Fiction":
print("Yes")
else:
print("No")
...gets me the No response, not the Yes. If I were to manually create the variable the long way...
Subject = "Fiction"
if Subject == "Fiction":
print("Yes")
else:
print("No")
...I get Yes.
But loaded from the text file, I don't. I need to create variables from text files, if possible, and I was trying to test that I got it right. I'm missing something.
So what am I doing wrong here?
You're probably reading a newline character (\n) at the end of the file and it is being saved to your variable...
In order to get rid of this character, use the .strip method, which removes all space characters (spaces, tabs, newlines, etc..) in front and after a string. Your code should look like this
file = open("Subject.txt", "r")
Subject = file.read().strip()
file.close()
This question already has answers here:
How to input a regex in string.replace?
(7 answers)
Closed 2 years ago.
I'm extremely new to coding and python so bear with me.
I want to remove all text that is in parenthesis from a text file. There are multiple sets of parenthesis with varying lengths of characters inside. From another similar post on here, I found
re.sub(r'\([^()]*\)', '', "sample.txt")
which is supposed to remove characters between () but does absolutely nothing. It runs but I get no error code.
I've also tried
intext = 'C:\\Users\\S--\\PycharmProjects\\pythonProject1\\sample.txt'
outtext = 'C:\\Users\\S--\\PycharmProjects\\pythonProject1\\EDITEDsample.txt'
with open("sample.txt", 'r') as f, open(outtext, 'w') as fo:
for line in f:
fo.write(line.replace('\(.*?\)', '').replace('(', " ").replace(')', " "))
which successfully removes the parenthesis but nothing inbetween them.
How do I get the characters between the parenthesis out?
EDIT: I was asked for a sample of sample.txt, these are it's contents:
Example sentence (first), end of sentence. Example Line (second), end
of sentence (end).
As you can see here, the function sub does not receive a filename as parameter, but actually it receives the text on which to work.
>>> re.sub(r'\([^()]*\)', '', "123(456)789")
'123789'
As for your second attempt, notice that string.replace does not take in REGEX expressions, only literal strings.
After parsing, I've got a lot of urls that have unfortunately joined together in one line. It will take a long time to re-parse, so I ask if there is a method as one long line with Url to turn into a multiple lines - 1 Url per line?
What i have:
'https:// url1.com/bla1','https:// url1.com/bla2',..thousands of urls..,'https:// url999.com/blaN'
What i need:
'https:// url1.com/bla-1',
'https:// url1.com/bla-2',
etc
'https:// url999.com/bla-N'
I've already tried to uncheck Line breaks in Python - Wrapping and Braces and check Ensure right margin is not exceeded - doesn't work
So how can i fix it?
Yes.
First set Code->Style->Wrapping and Braces->Method parameters/Method call arguments to wrap if long or chop down if long.
After that simply call reformat code on the line (Command+Alt+L).
Let's try a simple method, if I understand your query correctly. Read the first file, replace commas with newline character, and write the result to the same file.
urlsfile = open('test1.txt', 'r+') # in case you are getting the data from file itself
urls = urlsfile.readline()
urlsfile.close()
newlines = urls.replace(",", "\n") # otherwise replace newlines with the variable name that you are trying to write to the file
newfile = open('test1.txt','w+')
newfile.write(newlines)
newfile.close()
This question already has answers here:
Replacing instances of a character in a string
(17 answers)
Closed 5 years ago.
I tried to replace vowels and add il to them using this code but it didnt work, help!
line=input("What do you want to say?\n")
line = line.replace('e', 'ile')
line = line.replace('o', 'ilo')
line = line.replace('a', 'ila')
line = line.replace('i', 'ili')
line = line.replace('u', 'ilu')
line = line.replace('y', 'ily')
print (line)
But if you type a long sentence it stop working correctly.
could someone please help me?
Want to print "Hello world"
it prints:
Hililellililo wililorld
when should print Hilellilo Wilorld
Try replacing any occurrence of the letters you want with regex. Like this i.e:
import re
re.sub(r'[eE]', 'i$0', "Hello World")
You can replace any letter you want putting them inside the square brackets.
Additionally, that 'i$0' is the literal character 'i' and $0 the letter that was matched.
"Hello world".replace('e', 'ie')
But your question is not very clear, may be you mean something different.
Whenever you do multiple replacements after each other, you always need to be careful with the order in which you do them.
In your case put this replacement first:
line = line.replace('i', 'ili')
Otherwise it replaces the i's in the replacements that have been done before.
When you need to do many replacements it is often better to use an approach that avoids these problems.
One of them can be using regular expressions, as already proposed. Another is scanning the text from start to end for items to replace and replace each item when you find it during the scan and continue scanning after the replacement.
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 7 years ago.
I am a new to python programming. I've followed the "Learn python the hard way" book but that is based on python 2x.
def print_one_line(line_number,f):
print(line_number,f.readline())
In this function every time it prints A line and a new line.
1 gdfgty
2 yrty
3 l
I read the documentary that if i put a , (comma) after readline()
then it won't print a new \n.
Here is the documentary:
Why are there empty lines between the lines in the file? The
readline() function returns the \n that's in the file at the end of
that line. This means that print's \n is being added to the one
already returned by readline() fuction. To change this behavior simply add a ,
(comma) at the end of print so that it doesn't print its own .
When I run the file with python 2x then it is OK, but when I do it in python 3x then the newline is printed. How to avoid that newline in python 3x?
Since your content already contains the newlines you want, tell the print() function not to add any by using the optional end argument:
def print_one_line(line_number,f):
print(line_number,f.readline(), end='')
Beside the other ways, you could also use:
import sys
sys.stdout.write(f.readline())
Works with every Python version to date.
Rather than skipping the newline on output, you can strip it from the input:
print(line_number, f.readline().rstrip('\n'))