Difference detween line.rstrip("\n") and line.rstrip(os.linesep)? [duplicate] - python

This question already has answers here:
How can I remove a trailing newline?
(27 answers)
What is os.linesep for?
(2 answers)
Closed 1 year ago.
Real quick question, what is the difference between line.strip("\n") and line.rstrip(os.linesep)? Especially, when using them in the Fasta file and other bioinformatics fields.

line.strip("\n") removes the new-line character for UNIX and UNIX-like OSs. On the other side, line.strip(os.linesep) would automatically change to the correct new-line character for your current operation system (Mac OS, Linux, Windows, etc)

Related

What is \r in python? [duplicate]

This question already has answers here:
What is the difference between a "line feed" and a "carriage return"?
(4 answers)
What does "\r" do in the following script?
(4 answers)
Closed 2 years ago.
I am a newbie in the field of python. I recently came across \r (carriage return). It seems like it performs the same task as \n. Can anyone tell me the difference between \r and \n and specify the operation of \r?

How to suppress * as a wildcard, instead interpret as character? [duplicate]

This question already has answers here:
Prevent expansion of wildcards in non-quoted python script argument when running in UNIX environment
(5 answers)
Closed 3 years ago.
I am working with files that have an asterisk in the filenames.
I have a list of certain ones I am interested in but whenever I loop through them, the "*" in the name is interpreted as a wildcard.
For example: filenames are A*01:02 and A*02.
I only want to extract A*02 but the "*" is interpreted as a wildcard and both A*01:02 and A*02 are used.
How can I suppress the wildcard "*" and interpret it as a character instead?
Quote "A*02" or use a backslash as in A\*02.

How to get rid of "+" in a line? [duplicate]

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 3 years ago.
I have the txt file below and I want the remove the "+" if there is a + in front of the number.
+905061459318
+905458507534
+905437335094
I have tried almost all of the solutions that exist in Stackoverflow but still, I cannot remove the + from the line.
The code is here.
with open("numbers.txt") as f:
lines = [line.rstrip('\n') for line in open("numbers.txt")]
for line in lines:
if line.startswith("+"):
line.replace("+","")
else:
pass
You already have line.rstrip('\n'). Make that line.rstrip('\n').lstrip('+').

How to split a long python one-liner? [duplicate]

This question already has answers here:
Is it possible to break a long line to multiple lines in Python? [duplicate]
(7 answers)
Closed 5 years ago.
I have a long line that pylint is complaining is breaking pep8 E501
count_approvers = self.leave_not_required_user.company.leave_approvers.count()
How can I split this over 2 lines?
Configure your pylint for length of line. Or you can use '\' after '='
count_approvers = \
self.leave_not_required_user.company.leave_approvers.count()
You could use a temporary alias, if you may:
company = self.leave_not_required_user.company
count_approvers = company.leave_approvers.count()

Python - Spaces in Filenames [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to escape os.system() calls in Python?
Is there a Python method of making filenames safe (ie. putting \ infront of spaces and escaping ( , ), symbols) programatically in Python?
Spaces are already "safe" for Python in open(). As for os.system() and similar functions, use subprocess instead.
>>> import pipes
>>> pipes.quote("\&*!")
"'\\&*!'"

Categories

Resources