Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python strip with \n [duplicate]

Tags:

python

strip

This is my problem.

I'm trying to read a text file and then convert the lines into floats. The text file has \n and \t in it though I don't know how to get rid of it.

I tried using line.strip() but it didn't take it off and I got an error when I wanted to convert the stuff to floats. I then tried line.strip("\n") but that didn't work either. My program works fine when I take out the \t and \n from the text file, but it's part of the assignment to get it to work with them.

I really don't know why this isn't working. Thanks for any help.

like image 628
Mallard Duck Avatar asked Feb 19 '12 07:02

Mallard Duck


People also ask

Can you strip \n in Python?

Remove \n From the String in Python Using the str. strip() Method. In order to remove \n from the string using the str. strip() method, we need to pass \n and \t to the method, and it will return the copy of the original string after removing \n and \t from the string.

Does Strip remove N?

The strip() method removes whitespace by default, so there is no need to call it with parameters like '\t' or '\n'. However, strings in Python are immutable and can't be modified, i.e. the line.

How do you duplicate a line in Python?

Alternatively, you can press Ctrl+Shift+A , start typing the command name in the popup, and then choose it there. The duplicated line or multi-line selection is inserted below the original line or selection; the duplicated inline selection is inserted to the right of the original.


2 Answers

You should be able to use line.strip('\n') and line.strip('\t'). But these don't modify the line variable...they just return the string with the \n and \t stripped. So you'll have to do something like

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

That should work for removing from the start and end. If you have \n and \t in the middle of the string, you need to do

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

to replace the \n and \t with nothingness.

like image 97
austin1howard Avatar answered Oct 14 '22 16:10

austin1howard


The strip() method removes whitespace by default, so there is no need to call it with parameters like '\t' or '\n'. However, strings in Python are immutable and can't be modified, i.e. the line.strip() call will not change the line object. The result is a new string which is returned by the call.

As already mentioned, it would help if you posted an example from your input file. If there are more than one number on each line, strip() is not the function to use. Instead you should use split(), which is also a string method.

To conclude, assuming that each line contains several floats separated by whitespace, and that you want to build a list of all the numbers, you can try the following:

floats = [] with open(filename) as f:     for line in f:         floats.extend([float(number) for number in line.split()]) 
like image 27
Dan Gerhardsson Avatar answered Oct 14 '22 18:10

Dan Gerhardsson