I have this text file
VENDOR ITEM NUMBER WH ITEM DESCRIPTION PRODUCT NUMBER PRICE DISC % MAIN-WH ALT-WH BIN# --------------- ----- -- ------------------------------ --------------- --------- ------ --------- --------- ------ 0.00 EA 0.00 0 0 10.5PLC/TLED/26V/27K 14.5W 4PIN CFL REPL 2700K VERT 458406 20.00 EA 0.00 0 0 I68 I68 (00029 ) 10.5PLC/TLED/26V/30K 14.5W 4PIN CFL REPL 3000K VERT 458414 20.00 EA 0.00 3 0 PAYOFF I68 I68 (00029 ) 10.5PLC/TLED/26V/35K 14.5W 4PIN CFL REPL 3500K VERT 458422 20.00 EA 0.00 0 0 I68 I68 (00029 ) 10.5PLC/TLED/26V/40K 14.5W 4PIN CFL REPL 4000K VERT 458430 20.00 EA 0.00 0 0 I68 I68 (00029 ) I want to read each line item and get the item Number, description, Vendor product number and price.
I tried using this python code
def readInventoryFile(): # dataFile = open("inventoryFiles/INV.txt","r") with open('inventoryFiles/INV.txt') as dataFile: for lineItem in dataFile: itemProperties = lineItem.split(" ") while("" in itemProperties) : itemProperties.remove("") print(itemProperties) try: itemNum = itemProperties[0] itemDesc = itemProperties[1] partNumb = itemProperties[2] price = itemProperties[3] itemSummry = { "Name": itemDesc, "Price": price, "PN": partNumb, } print(lineItem, "\n ",itemProperties,"\n Summary ",itemSummry) except Exception as e: print(e) The code partially works but it's hard to split the line by spaces or other factors because there are spaces that separated and within the content of each line. How could I get the desired product properties?
pandasand reading "fixed-width format"... pandas.pydata.org/pandas-docs/stable/reference/api/… and even then, you'll have to parse out some dataVENDORpart ofPRODUCT NUMBER. Is(00029 )belonged toBIN#. The first line has missing fields, is it correct? The file has 10 connected dashed but it has less number of header fields. What i see may not be what you see though. Perhaps you could explain number of fields and positions a field starts and end ends.