weemo
weemo

Reputation: 7

Python - CSV to extract data from text file into a CSV file

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

Upvotes: 0

Views: 373

Answers (1)

tk.
tk.

Reputation: 626

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

Upvotes: 1

Related Questions