• Register
0 votes


What is the Python equivalent of Perl's chomp function, which removes the last character of a string if it is a newline?


5 1
200 points

1 Answer

0 votes



The strip() technique evacuates whitespace as a matter of course, so there is no compelling reason to call it with parameters like '\t' or '\n'. In any case, strings in Python are unchanging and can't be altered, for example the line. strip() call won't change the line object. The outcome is another string which is returned by the call. 

As of now referenced, it might be ideal on the off chance that you posted a model from your information document. In the event that there are more than one number on each line, strip() isn't the capacity to utilize. Rather you should utilize split(), which is likewise a string technique. 

To finish up, accepting that each line contains a few buoys isolated by whitespace, and that you need to fabricate a rundown of the considerable number of numbers, you can attempt the accompanying:

floats = []
with open(filename) as f:
    for line in f:
        floats.extend([float(number) for number in line.split()])


Another solution:

You ought to have the option to utilize line.strip('\n') and line.strip('\t'). In any case, these don't adjust the line variable...they simply return the string with the \n and \t stripped. So you'll need to accomplish something like

line = line.strip('\n')
line = line.strip('\t')

That should work for expelling from the beginning and end. On the off chance that you have \n and \t in the string, you have to do

line = line.replace('\n','')
line = line.replace('\t','')
460 points

Related questions

0 votes
1 answer 7 views
I require to remove all the special characters, puntuation and and speces from a string?
asked Sep 14 Daniel Anderson 4k points
1 vote
1 answer 28 views
Problem: I have got a string list that is : ex_list = ['yo\no', 'illi\nes', 'by\nest', 'fork\ner', 'pick\ner'] I tried to remove /n from string using python. So, I wrote this code: ex_list = ['yo\no', 'illi\nes', 'by\nest', 'fork\ner', 'pick\ner'] print("The ... "main.py", line 5, in <module> res.append(sub.replace("\n", "")) NameError: name 'res' is not defined Can any one help me to solve this?
asked May 12 samhaz 5k points
1 vote
1 answer 30 views
Problem: Hi all, I am a novice in python programming. I got a problem in the textbook exercise I was learning from. It says to remove all special characters from string python. I&rsquo;ve tried several ways that didn&rsquo;t work for me. Unfortunately, I removed all the char from my string. So, what is the best practice to do it if it is doable? Thanks for your valuable time. Best regards.
asked Apr 5 Gavin 15.3k points
1 vote
1 answer 32 views
I am trying to write a script to print specific words after a particular string. Here is the input file Theyare "playing in the ground", with friends Theyare "going to Paris", with family Theyare "motivating to learn new things", by themselves In the output I am trying to select "are" as the keyword ... open ('input.txt', 'r') as f: for lines in f: a = re.search(r'\bare', f): if a: print (lines)
asked Jul 15 anonymous
2 votes
1 answer 17 views
Problem: Well, let&rsquo;s assume I have a string in python and I am willing to remove all the duplicates characters from my string. Suppose my string is &ldquo;Australia&rdquo; the program should return &ldquo;Austrli&rdquo; So, my question is simple. How can I remove duplicate characters in a string python?
asked Apr 5 Gavin 15.3k points