acclivity

pyRegexForProductList

Feb 10th, 2022 (edited)
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. import re
  2.  
  3. fin = open("ProductList.txt")
  4. data = fin.readlines()
  5.  
  6. for index, line in enumerate(data):
  7.     if line.startswith("- ") and line.endswith(" -\n"):
  8.         line = line[2:-3]
  9.         descr = data[index - 1].strip()
  10.                 fields = re.split(r'([a-zA-Z0-9 ]+)\((\d+)\) ([0-9$.]+) ([A-Za-z &,.\w]+)', line)
  11.         print("Description:", descr)
  12.         print("Quantity in Stock:", fields[2])
  13.         print("Price:", fields[3])
  14.         print("Category: ", fields[4].strip())
  15.         print("\n")
  16.  
  17. # Output
  18. # Description: Spiegelau Authentis Collection Burgundy Wine Glasses, Set of 6
  19. # Quantity in Stock: 0
  20. # Price: $12.93
  21. # Category:  Uncategorized
  22. #
  23. #
  24. # Description: Sharp LC-20E1UB 20-Inch LCD Flat-Panel TV (Black)
  25. # Quantity in Stock: 1
  26. # Price: $279.93
  27. # Category:  Uncategorized
  28. #
  29. #
  30. # Description: Gain 150-oz Original Liquid Laundry Detergent
  31. # Quantity in Stock: 1
  32. # Price: $15.22
  33. # Category:  Health & Household
  34.  
  35. # =========================================================================
  36. # Input file:-
  37. Sabatier Precision 14-Piece Stainless-Steel Knife, Block Set
  38. - Out of stock (0) $15.28 Uncategorized –
  39. No
  40. Published
  41. 2022/02/07 at 8:51 pm
  42.  
  43. Spiegelau Authentis Collection Burgundy Wine Glasses, Set of 6
  44. - Out of Stock (0) $12.93 Uncategorized -
  45. No
  46. Published
  47. 22/02/07 at 6:51 pm
  48.  
  49. Sharp LC-20E1UB 20-Inch LCD Flat-Panel TV (Black)
  50. - In Stock (1) $279.93 Uncategorized -
  51. No
  52. Published
  53. 2022/02/07 at 4:59 pm
  54.  
  55. Gain 150-oz Original Liquid Laundry Detergent
  56. - In Stock (1) $15.22 Health & Household -
  57. No
  58. Published
  59. 2022/02/07 at 1:52 pm
Add Comment
Please, Sign In to add comment