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
  delete specific row of entries jacksfrustration 3 324 Feb-13-2024, 11:13 PM
Last Post: deanhystad
  doing string split with 2 or more split characters Skaperen 22 2,317 Aug-13-2023, 01:57 AM
Last Post: Skaperen
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,426 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into </>? Winfried 0 1,451 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,086 Aug-19-2022, 08:07 PM
Last Post: Winfried
  Pymysql delete specific rows in tableview stsxbel 2 1,049 Aug-18-2022, 09:50 AM
Last Post: ibreeden
  Adding string after every 3rd charater [SOLVED] AlphaInc 2 1,208 Jul-11-2022, 09:22 AM
Last Post: ibreeden
  Delete multiple lines from txt file Lky 6 2,201 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  Delete empty text files [SOLVED] AlphaInc 5 1,510 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  Trying to delete rows above a specific datetime value cubangt 19 11,001 May-09-2022, 08:57 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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