Advertisement
Python253

bin2csv

Mar 11th, 2024
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: bin2csv.py
  4. # Version: 1.0.1
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script converts a binary file (.bin) to a CSV file (.csv).
  10. It assumes that each line in the binary file represents a separate record, & it decodes each line from UTF-8 before writing it to the CSV file.
  11.  
  12. Requirements:
  13. - Python 3.x
  14. - No additional external libraries are required.
  15.  
  16. Usage:
  17. 1. Save this script as 'bin2csv.py'.
  18. 2. Ensure your binary file ('example.bin') is in the same directory as the script.
  19. 3. Run the script.
  20. 4. The converted CSV file ('bin2csv.csv') will be generated in the same directory.
  21.  
  22. Note: Adjust the 'bin_filename' and 'csv_filename' variables in the script as needed.
  23. """
  24.  
  25. import csv
  26.  
  27. def bin_to_csv(bin_filename, csv_filename):
  28.     with open(bin_filename, 'rb') as binfile, open(csv_filename, 'w', newline='') as csvfile:
  29.         csvwriter = csv.writer(csvfile)
  30.         for line in binfile:
  31.             # Assuming each line in the binary file is a separate record
  32.             csvwriter.writerow([line.decode('utf-8').strip()])
  33.  
  34. if __name__ == "__main__":
  35.     bin_filename = 'example.bin'
  36.     csv_filename = 'bin2csv.csv'
  37.     bin_to_csv(bin_filename, csv_filename)
  38.     print(f"Converted '{bin_filename}' to '{csv_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement