Skip to content
Advertisement

Remove specific string char at the beginning of each lines of a txt file using python

I’m currently working on a script in python. I want to convert an xls file into a txt file but I also want to clean and manage the data. In the xls files, there’s 4 columns which does interest me. Here is a sample of the txt I get from the conversion :

JavaScript

To get this result I used this script :

JavaScript

That’s a good beginning but now I’d like to remove the “OPEN 0” of each file but I don’t know how to do it because it could be placed eather in the A:B columns or in the C:D columns. Also the result in the txt is not vertically aligned :( . Do you have any ideas ?

Thanks a lot

Advertisement

Answer

Please find below the updated code. This is the input I took, added a couple of rows to ensure it works fine..

input xlsx

Few changes:

  1. read_excel has header=None as your data did not have header
  2. While writing to my_txt, I added index=False, header=False so that there is no index added to the txt file and no headers. This way your output file will only have data
  3. I used the dataframe to clean the OPEN and ‘0’ when they are in columns A:B or C:D ONLY. Otherwise, leave them alone.
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement