If you can comment out code and your program still works, then yes, that code was optional.
.strip() with no arguments (or None as the first argument) removes all whitespace at the start and end, including spaces, tabs, newlines and carriage returns. Leaving it in doesn't do any harm, and allows your program to deal with unexpected extra whitespace inserted into the file.
For example, by using .strip(), the following two lines in a file would lead to the same end result:
foo\tbar \n
foo\tbar\n
I'd say leave it in.
Answer from Martijn Pieters on Stack OverflowI want to strip a certain word off of a string. I tried doing this.
string= "Germany Income and Capital Tax Treaty"
string = string.lower().strip('germany')
print(string)I expected to get:"income and capital tax treaty"
Instead I get"income and capital tax treat"
What is it that I do not understand. Also lstrip and rstrip would not provide a solution as I am writing this in a function where a complete match with the word would need to get stripped off of 'either' end.
Any help?
String.strip() in Python - Stack Overflow
Python strip with \n - Stack Overflow
.strip function not working in VS code (python)
strip() function usage in Python - Stack Overflow
If you can comment out code and your program still works, then yes, that code was optional.
.strip() with no arguments (or None as the first argument) removes all whitespace at the start and end, including spaces, tabs, newlines and carriage returns. Leaving it in doesn't do any harm, and allows your program to deal with unexpected extra whitespace inserted into the file.
For example, by using .strip(), the following two lines in a file would lead to the same end result:
foo\tbar \n
foo\tbar\n
I'd say leave it in.
In this case, you might get some differences. Consider a line like:
"foo\tbar "
In this case, if you strip, then you'll get {"foo":"bar"} as the dictionary entry. If you don't strip, you'll get {"foo":"bar "} (note the extra space at the end)
Note that if you use line.split() instead of line.split('\t'), you'll split on every whitespace character and the "striping" will be done during splitting automatically. In other words:
line.strip().split()
is always identical to:
line.split()
but:
line.strip().split(delimiter)
Is not necessarily equivalent to:
line.split(delimiter)
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.
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()])