• Register
0 votes
91 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 33 views
33 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 12 views
12 views
I require to remove all the special characters, puntuation and and speces from a string?
asked Sep 14, 2020 Daniel Anderson 4k points
1 vote
1 answer 33 views
33 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, 2020 Gavin 15.3k points
1 vote
1 answer 52 views
52 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, 2020 anonymous
0 votes
1 answer 13 views
13 views
Problem: Hello! I am a beginner, and my teacher gave me an assignment in which I have to write a program that takes a string as input from the user. If the string contains any consecutive duplicate characters then my program should remove all the consecutive occurrences of that ... . I am weak in programming and have almost no idea how I can do that. Can anybody here help me to solve this problem?
asked Jan 2 Code Learner 5.1k points
2 votes
1 answer 30 views
30 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, 2020 Gavin 15.3k points
0 votes
1 answer 45 views
45 views
Problem : How to remove all occurrences of a value from a list? I tried in Python remove() will remove the first occurrence of value in a list. How to remove all occurences?
asked Oct 22, 2019 peterlaw 6.9k points
0 votes
1 answer 5 views
5 views
Problem: Hi there! I just got an assignment in which I had to remove the first and last character from a string in Java. I did the task using substrings. I want to know what the simplest way is to do so? Also, mention other alternatives to using the substring method. Thanks!
asked Jan 8 Code Learner 5.1k points
1 vote
1 answer 10 views
10 views
Problem: Many of my friends told me that Python is a better programming language for beginners. It is easy to pick up. I was convinced by their words and started learning Python. As days are passing I am discovering new things and it keeps attracting me. Well, today I was ... better way to do that job! I completely understand that I didn&rsquo;t do it in an appropriate way. What do you say, guys?
asked May 31, 2020 adamSw 11.3k points
1 vote
1 answer 29 views
29 views
Problem: Hello Kodlogs, I am a student of the Python programming language. I am taking an online course to learn it. Every day they take classes on each topic and provide some simple problems to solve as an assignment. I had a class on String type yesterday and have got ... me a solution to make it happen for me. Please keep it as simple as possible, it will help me to understand easily. Thanks.
asked Mar 29, 2020 Gavin 15.3k points