Can you strip \n in python?
This is my problem. Show I'm trying to read a text file and then convert the lines into floats. The text file has I tried using I really don't know why this isn't working. Thanks for any help.
John Machin 79.3k11 gold badges137 silver badges182 bronze badges asked Feb 19, 2012 at 7:07
5
You should be able to use
That should work for removing from the start and end. If you have
to replace the answered Feb 19, 2012 at 7:17
austin1howardaustin1howard 4,6853 gold badges19 silver badges22 bronze badges 3 The 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, 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:
answered Feb 19, 2012 at 17:27
You can use:
to get a python list with only the field values for all the lines of data. answered Sep 7, 2016 at 13:51
JobelJobel 6136 silver badges12 bronze badges How about using a python regex pattern?
And for your case of line.strip() will not work because Python removes only the leading and trailing characters
answered Feb 19, 2012 at 7:52
sransarasransara 3,4062 gold badges18 silver badges21 bronze badges pythons csv library is good for this. http://docs.python.org/library/csv.html CSV = comma seperated values, but if you set the delimiter = \t, then it works for tab separated values too. answered Feb 19, 2012 at 7:15
Rusty RobRusty Rob 15.6k8 gold badges94 silver badges111 bronze badges Often, depending on the way you read the lines, in order to get rid of \n from myline, you can take myline[:-1] since \n is the last character of myline. For the '\t' you can use replace() or strip() answered Feb 19, 2012 at 7:32
jimifikijimifiki 5,2241 gold badge32 silver badges56 bronze badges If you're trying to convert lines of floats separated by tab characters, then just Maybe you need to answered Feb 19, 2012 at 7:51
BenBen 64.1k20 gold badges124 silver badges168 bronze badges Not the answer you're looking for? Browse other questions tagged python strip or ask your own question.Does strip get rid of New line?The strip() function is used to remove both trailing and leading newlines from the string that it is being operated on. It also removes the whitespaces on both sides of the string. The following code uses the strip() function to remove a newline character from a string in Python.
Can I print \n in Python?The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end =
What can you strip in Python?The Strip() method in Python removes or truncates the given characters from the beginning and the end of the original string. The default behavior of the strip() method is to remove the whitespace from the beginning and at the end of the string.
How do you break a new line in a string in Python?In Python, the new line character “\n” is used to create a new line. When inserted in a string all the characters after the character are added to a new line.
|