Reputation: 101
I am using python to read headers from SAC but am having trouble removing whitespace. I want to remove the space before the next station name e.g RPZ, TOZ, URZ. This is my code:
for s in stations:
tr=st[0]
h=tr.stats
stnm = h.station
tt = h.sac.t1
print>>f,stnm, 'P', '1', tt,
I want the output to look like this:
DSZ P 1 53.59RPZ P 1 72.80TOZ P 1 40.25URZ P 1 32.26
Then to go onto a new line after 32.26
. This is why I have the comma after tt
.
However it currently outputs like this with the unwanted space before RPZ
, TOZ
and URZ
:
DSZ P 1 53.59 RPZ P 1 72.80 TOZ P 1 40.25 URZ P 1 32.26
Any suggestions? I have tried x.strip()
, but I get the
AttributeError: 'list' object has no attribute 'strip'.
Upvotes: 1
Views: 164
Reputation: 181077
Just as an alternative to Martin's answer, you could also (for Python 2.6+ import and) use the print function, something like;
# import only needed for Python2
from __future__ import print_function
print(stnm, 'P', '1', tt, end='', file=f)
Upvotes: 0
Reputation: 1124808
The print
statement is adding the spaces; if you want the space to be dropped, don't print
but use f.write()
instead:
f.write('{} P 1 {}'.format(stnm, tt))
This uses string formatting with str.format()
to create the same output format, but now no space will be written following the tt
value.
Upvotes: 1