Python Forum

Full Version: [SOLVED] Delete specific characters from string lines
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
[Solved]
I have this file right here, which is an extract of a much bigger file.

Quote:CSCIR, 30, 0, 0
CSYS, 0
MAT , 2306
TYPE, 2741
REAL, 2749
NBLOCK,6,SOLID, 1574331, 390115
(3i9,6e21.13e3) EUREKA
1163065 0 0 1.2309330490000E+001 1.4094185190000E+002-2.6164289660000E+000
1163066 0 0 1.6908245310000E+001 1.3347532020000E+002-3.5939584870000E+000
1163067 0 0 1.8897693910000E+001 1.3494341620000E+002-4.0168288420000E+000
1163068 0 0 1.4443393310000E+001 1.4091121090000E+002-3.0700380210000E+000
1163069 0 0 1.4714475250000E+001 1.3618873440000E+002-3.1276582650000E+000
1163070 0 0 1.7605477220000E+001 1.3609469910000E+002-3.7421597040000E+000
1163071 0 0 1.2177877690000E+001 1.4552741340000E+002-2.5884878110000E+000
1163072 0 0 1.2254404380000E+001 1.4326778460000E+002-2.6047540600000E+000

I already made code for Python to detect the starting point thanks to help from this forum (thanks a lot btw, advice worked wonders).
(EUREKA) marks this starting point.
Now I simply want to remove one 0 after E+ in every instance.

Example:

1163065 0 0 1.2309330490000E+001 1.4094185190000E+002-2.6164289660000E+000
becomes
1163065 0 0 1.2309330490000E+01 1.4094185190000E+02-2.6164289660000E+00
Here a hint.
>>> line = '1163065 0 0 1.2309330490000E+001 1.4094185190000E+002-2.6164289660000E+000'
>>> line = line[:-1]
>>> line
'1163065 0 0 1.2309330490000E+001 1.4094185190000E+002-2.6164289660000E+00'
So when read(line bye line) do a check that eg that E+000 is in line then do operation over else: do nothing.
I'm guessing line = line[-1] means that Python will delete the first character starting from the end?
Meaning that to delete the ones in the middle and left, I'd have to do something like line = line[-1,-20,40] assuming the zeros there are on the 20th and 40th spot.
Is this correct?
(Oct-21-2021, 09:54 AM)EnfantNicolas Wrote: [ -> ]Meaning that to delete the ones in the middle and left, I'd have to do something like line = line[-1,-20,40] assuming the zeros there are on the 20th and 40th spot.
I did read it to quick and only removed last one in last E+000.
regex is easier for this task when need to change in serval places.
>>> import re
>>> 
>>> line = '1163065 0 0 1.2309330490000E+001 1.4094185190000E+002-2.6164289660000E+000'
>>> result = re.sub(r"E\+00", r"E+0", line)
>>> result
'1163065 0 0 1.2309330490000E+01 1.4094185190000E+02-2.6164289660000E+00'
Yes I did something similar without importing regex.

                copy.write(line.replace("E+0", "E+"))