简体   繁体   中英

How to remove a certain character after the extraction of a part, using python

I have text file, which looks like this:

.
.
.
-----------------------
first ATOMIC CHARGES
-----------------------
   0 C :   -0.157853
   1 C :   -0.156875
   2 C :   -0.143714
   3 C :   -0.140489
   4 S :    0.058926
   5 H :    0.128758
   6 H :    0.128814
   7 H :    0.142420
   8 H :    0.140013
My charges :   -0.0000000

------------------------
.
..
.

I used this script below in order to extract a specific part.

with open('FILE.txt', 'rb') as f:
     textfile_temp = f.read()

     print textfile_temp.split('first ATOMIC CHARGES')[1].split("My charges :   -0.0000000")[0]

my output is:

-----------------------
   0 C :   -0.157853
   1 C :   -0.156875
   2 C :   -0.143714
   3 C :   -0.140489
   4 S :    0.058926
   5 H :    0.128758
   6 H :    0.128814
   7 H :    0.142420
   8 H :    0.140013

my goal is to remove the "-----------------------" character, and my output will be like this:

   0 C :   -0.157853
   1 C :   -0.156875
   2 C :   -0.143714
   3 C :   -0.140489
   4 S :    0.058926
   5 H :    0.128758
   6 H :    0.128814
   7 H :    0.142420
   8 H :    0.140013

To get rid of that line, try:

stringy = stringy.replace("--", "").strip() # assuming an even number of dashes

This will get rid of all the extra dashes and the newline.

Or you could break the stringy into a list of lines, then exclude the first line, with

stringy = '\n'.join(stringy.splitlines()[1:])

or brute force:

stringy = stringy.replace('-----------------------\n', '')

Or just change:

print textfile_temp.split('first ATOMIC CHARGES')[1].split("My charges :   -0.0000000")[0]

to

print textfile_temp.split('first ATOMIC CHARGES')[1].split("My charges :   -0.0000000")[0].replace('-----------------------\n', '')

Using triple '

with open('data.txt', 'r') as f:
    textfile_temp = f.read()

    print(textfile_temp.split('''first ATOMIC CHARGES
-----------------------''')[1].split('My charges :   -0.0000000')[0])

@Hamza allal In this, simplest you can just find two indexes from the file data,

  1. First occurrence of 0 number
  2. Index of ' My charges' string

zero_ind = file_data.find("0")

str_ind = file_data.find("My charges", zero_ind)

file_data[zero_ind:str_ind].split("\\n")

Then just split the file data using '\\n' you will get all items you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM