0

I have a text file that is not completely formated, however it has column designated.

Code            Description          Unit          Retail
1000MADB90      Taupe 2X2            12X12         5.90
1002MOPAS       Grey Stilato         12X12         12.09

then some junk and then again

      Code            Description          Unit        Retail
    1050QADD         Black 2X2            12X12         2.12
    1002MOPAS       Red velvet            12X12         1.11

I need to be able to have it formated in a csv file and without the junk before and after. So, I would like to keep the column names and info after them until it hits junk and jumps into the other column that starts with Code. I have tried a few (12 to be precise) of examples found on stackeroverflow but cant seem to get it as it is formatted for the likes of Excel (csv). Also the columns in the text file vary in size and alignment (dunno if that matters)

I am no programmer but I am looking for an easy way to convert a catalog to input it in a POS System. I appreciate the help

3
  • 1
    Consider using pandas if your process allows you to add this package Commented Oct 25, 2013 at 21:56
  • OK, the variation of the size of the columns does matter somewhat, is there any common pattern that they adhere to? Is it always Code Description Unit Retail and then something like 1000MADB90 Taupe 2X2 12X12 5.90 below? Commented Oct 25, 2013 at 21:56
  • Yes, always Code Description Unit Retail and other columns which I am not interested..then useful data then junk..then it starts again with Code Description Unit Retail..thanks for the help Commented Oct 25, 2013 at 22:07

1 Answer 1

1

An easy way to do it (provided you know an easy way to identify junk) :

with open('originalfile.csv','r') as f:
  with open('newfile.csv','w') as new_file:
    for row in f:
      if not is_junk(row):
        new_file.write(row)


def is_junk(row):
  return not row.strip() or ( not ( ("Retail" in row ) or is_float(row.split()[-1]) ) )

def is_float(str):
  try:
    float(str)
    return True
  except ValueError:
    return False
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.