Python Forum
[SOLVED] Delete specific characters from string lines
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Delete specific characters from string lines
#1
Question 
[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
Reply
#2
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.
Reply
#3
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?
Reply
#4
(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'
Reply
#5
Yes I did something similar without importing regex.

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace String in multiple text-files [SOLVED] AlphaInc 5 1,039 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  Replace String with increasing numer [SOLVED] AlphaInc 13 1,220 Aug-07-2021, 08:16 AM
Last Post: perfringo
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 517 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  [Solved] Trying to read specific lines from a file Laplace12 7 1,211 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  How to extract specific key value pair from string? aditi06 0 868 Apr-15-2021, 06:26 PM
Last Post: aditi06
  reading lines from a string [Solved] ebolisa 14 2,120 Mar-28-2021, 08:16 PM
Last Post: perfringo
  Extract continuous numeric characters from a string in Python Robotguy 2 900 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Split Characters As Lines in File quest_ 3 1,033 Dec-28-2020, 09:31 AM
Last Post: quest_
  Delete lines from a string that contains a keyword JellyCreeper6 4 930 Oct-30-2020, 10:46 AM
Last Post: JellyCreeper6
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 1,213 Oct-19-2020, 04:16 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020