Advertisement
Guest User

Untitled

a guest
Oct 28th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. import csv
  2. from itertools import zip_longest
  3. def grouper(iterable, n, fillvalue=None):
  4. args = [iter(iterable)] * n
  5. return zip_longest(*args, fillvalue=fillvalue)
  6.  
  7. def remove_title(s, title):
  8. """if s starts with the title, erase it; otherwise, do nothing"""
  9. if s.startswith(title):
  10. s = s[len(title):]
  11. return s
  12.  
  13. def sanitize(s):
  14. """
  15. Remove newlines from the string and replace them with something else.
  16. Useful if you want each row in your csv file to take up exactly one line.
  17. If you don't care about that, then you don't need to call this later.
  18. """
  19. return s.replace("\n", "\\n")
  20.  
  21. with open("contentfile.txt", encoding="utf8") as file:
  22. paragraphs = file.read().split("\n\n")
  23.  
  24.  
  25. with open("output.csv", "w", newline='') as csvfile:
  26. fieldnames = ["title", "desc", "shortdesc", "tech", "spec"]
  27. writer = csv.DictWriter(csvfile, fieldnames)
  28. writer.writeheader()
  29.  
  30. for description_paragraph, tech_paragraph, spec_paragraph in grouper(paragraphs, 3):
  31. name, _, description_and_link = description_paragraph.partition("\n")
  32. short_description, _, link = description_and_link.rpartition("\n")
  33.  
  34. tech = remove_title(tech_paragraph, "TECHNOLOGY\n")
  35. specs = remove_title(spec_paragraph, "SPECIFICATIONS\n")
  36.  
  37. writer.writerow({
  38. "title": name,
  39. "desc": link,
  40. #these next three may contain newlines, so call `sanitize` on them if you don't wnat those newlines showing up in the csv.
  41. "shortdesc": sanitize(short_description),
  42. "tech": sanitize(tech),
  43. "spec": sanitize(specs)
  44. })
  45.  
  46.  
  47. """
  48. Result:
  49.  
  50. title,desc,shortdesc,tech,spec
  51. Intimus pacmaster s,https://nonagon-paddlefish-5ywp.squarespace.com/s/IS_PacMasterS_VS_01-18_en.pdf,The PacMaster S packaging material shredders allow businesses to recycle their cardboard packaging in a unique way.,Shreds old and obsolete cardboard to robust packaging material\nWell designed controls: lockable power switch is also an emergency stop switch\nSingle pass operation\nLockable master switch\nSize guide on the work surface to ensure accurate cutting of the cardboard to customise the stuffing mats to required format\nExtremely robust steel plate housing for heavy duty use at the packing station,"Dimensions (H x W x D): 37.4"" x 27.1"" x 18.5"" \nCutting Speed: 7.1 in/sec\nSupply Voltage: 400 V\nNoise level (dB(A)): 62-68.5\nWeight: 308.6 lbs"
  52. HSM SHREDSTAR S10 ,https://nonagon-paddlefish-5ywp.squarespace.com/s/HSM-shredstar-S10-1_4__1042123-1.pdf,"DOCUMENT SHREDDER\nData protection for everyone. \nThis modern and compact strip cut document shredder with a separate CD cutting unit destroys your data reliably and quietly, both at home or in a small office. ","TECHNOLOGy\nAutomatic reverse eliminates paper jams. \nSeparate CD cutting unit with own waste container for sorted disposal.\nQuiet operation minimizes noise development in the workplace.\nLED indicator shows the device’s operating status: Power, Overload, Overheat.\nLight barrier for automatic start/stop.\nLevel indicator through inspection window in the waste container. \nEffortless emptying of the waste container via the removable housing top.","Dimensions (H x W x D): 17.5” x 13.6” x 9.6”\nColour: White \nMotor: 125W\nContainer/Collection Volume: 4.8 gal\nIntake width/ working width: 8.7 in\nVoltage/frequency: 115 V / 60 Hz \nCutting size: 1/4""\nCutting type: strip\nCutting capacity (in sheets): 10\nSecurity level: P-2/O-1/T-1/E-2\nCutting Capabilities: Paper, Staples & paperclips, Credit & customer cards, CDs & DVDs\nWeight (lbs): 8.6"
  53. HSM SHREDSTAR X5 ,https://nonagon-paddlefish-5ywp.squarespace.com/s/HSM-shredstar-X5-3_16_-x-1-1_8__1043123.pdf,DOCUMENT SHREDDER\nData protection for the home or the small office. This modern and compact cross cut document shredder with a separate CD cutting unit shreds data in the workplace.,"TECHNOLOGy\nAutomatic reverse eliminates paper jams. \nSeparate CD cutting unit with own waste container for sorted disposal.\nQuiet operation minimizes noise development in the workplace.\nLED indicator shows the device’s operating status: Power, Overload, Overheat.\nLight barrier for automatic start/stop.\nLevel indicator through inspection window in the waste container. \nEffortless emptying of the waste container via the removable housing top.","Dimensions (H x W x D): 17.5” x 13.6” x 9.6”\nColour: White \nMotor: 125W\nContainer/Collection Volume: 4.8 gal\nIntake width/ working width: 8.7 in\nVoltage/frequency: 115 V / 60 Hz \nCutting size: 3/16” x 1 1/8”\nCutting type: cross\nCutting capacity (in sheets): 5\nSecurity level: P-4/O-1/T-1/E-2\nCutting Capabilities: Paper, Staples & paperclips, Credit & customer cards, CDs & DVDs\nWeight (lbs): 9.3"
  54. HSM SHREDSTAR X6pro ,https://nonagon-paddlefish-5ywp.squarespace.com/s/HSM-shredstar-X6pro-1_16_-x-5_8__1046113.pdf,DOCUMENT SHREDDER\nReliable and user-friendly micro cross cut document shredder with the highest security level in the shred star series. With a separate CD cutting unit for use in the workplace.,TECHNOLOGy\nAutomatic return eliminates paper jams. \nSeparate CD cutting unit with own waste container for sorted disposal.\nQuiet operation minimizes noise development in the workplace.\nLED indicator shows the operating state of the device\nLight barrier for automatic start/stop.\nLevel indicator through inspection window in the waste container. \nEffortless emptying of the waste container via the removable housing top.,"Dimensions (H x W x D): 18.3” x 13.6” x 9.6”\nColour: White \nMotor: 160W\nContainer/Collection Volume: 5.3 gal\nIntake width/ working width: 8.7 in\nVoltage/frequency: 115 V / 60 Hz \nCutting size: 1/16” x 5/8”\nCutting type: micro\nCutting capacity (in sheets): 6\nSecurity level: P-5/O-1/T-1/E-2\nCutting Capabilities: Paper, Staples & paperclips, Credit & customer cards, CDs & DVDs\nWeight (lbs): 13.9"
  55. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement