• Register
0 votes
102 views

Problem:

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

Please log in or register to answer this question.

1 Answer

0 votes

 

Solution:

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','')
3
460 points

Related questions

1 vote
1 answer 48 views
48 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, 2020 samhaz 5k points
0 votes
1 answer 4 views
4 views
Problem: how to I Solve this: find index of substring in string python HELP! I need help understanding this: find index of substring in string python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 10 Mashhoodch 13k points
0 votes
1 answer 3 views
3 views
Problem: Convert list of numbers to string python.
asked 3 days ago charles mathews 5.5k points
0 votes
1 answer 4 views
4 views
Problem: Need help with the conversion of all strings to integers. Convert int list to string list python.
asked Mar 23 PkGuy 13.1k points
0 votes
1 answer 7 views
7 views
Problem: Hi! Can some one help me with this ? Python convert list of ints to string .
asked Mar 21 PkGuy 13.1k points
0 votes
1 answer 2 views
2 views
Problem: how to I Solve this: convert list of characters to string python HELP! I need help understanding this: convert list of characters to string python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 10 Mashhoodch 13k points
0 votes
1 answer 6 views
6 views
Problem: how to I Solve this: convert string to list of characters python HELP! I need help understanding this: convert string to list of characters python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 8 Mashhoodch 13k points
0 votes
1 answer 9 views
9 views
Problem: How to I Solve this: python convert string to list of chars HELP! I need help understanding this: python convert string to list of chars Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 7 Mashhoodch 13k points
0 votes
1 answer 9 views
9 views
Problem: How to I Solve this: convert list of integers to string python HELP! I need help understanding this: convert list of integers to string python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 3 Mashhoodch 13k points
0 votes
1 answer 17 views
17 views
I require to remove all the special characters, puntuation and and speces from a string?
asked Sep 14, 2020 Daniel Anderson 4k points