fork download
  1. #!/usr/bin/env python3
  2.  
  3. import sys
  4. import json
  5. import csv
  6.  
  7. # Read CSV header
  8. header = next(sys.stdin).strip().split(',')
  9. header = [h.strip() for h in header]
  10.  
  11. # Read and process each row
  12. for line in sys.stdin:
  13. line = line.strip()
  14.  
  15. if not line:
  16. continue
  17.  
  18. try:
  19. values = next(csv.reader([line]))
  20.  
  21. if len(values) != len(header):
  22. continue
  23.  
  24. record = dict(zip(header, values))
  25.  
  26. # Convert data types
  27. record["transaction_id"] = int(record["transaction_id"])
  28. record["quantity"] = int(record["quantity"])
  29. record["price"] = float(record["price"])
  30.  
  31. # Output JSON
  32. print(json.dumps(record))
  33.  
  34. except (ValueError, StopIteration):
  35. continue
Success #stdin #stdout 0.14s 15624KB
stdin
transaction_id,user_id,product_id,quantity,price,timestamp
1001,user_A,prod_X,2,50.00,2023-01-01T12:00:00Z
1002,user_B,prod_Y,1,120.50,2023-01-01T12:05:00Z
1003,user_C,prod_Z,3,75.00,2023-01-01T12:10:00Z
1004,user_A,prod_Y,2,120.50,2023-01-01T12:15:00Z
1005,user_B,prod_X,1,50.00,2023-01-01T12:20:00Z
stdout
{"transaction_id": 1001, "user_id": "user_A", "product_id": "prod_X", "quantity": 2, "price": 50.0, "timestamp": "2023-01-01T12:00:00Z"}
{"transaction_id": 1002, "user_id": "user_B", "product_id": "prod_Y", "quantity": 1, "price": 120.5, "timestamp": "2023-01-01T12:05:00Z"}
{"transaction_id": 1003, "user_id": "user_C", "product_id": "prod_Z", "quantity": 3, "price": 75.0, "timestamp": "2023-01-01T12:10:00Z"}
{"transaction_id": 1004, "user_id": "user_A", "product_id": "prod_Y", "quantity": 2, "price": 120.5, "timestamp": "2023-01-01T12:15:00Z"}
{"transaction_id": 1005, "user_id": "user_B", "product_id": "prod_X", "quantity": 1, "price": 50.0, "timestamp": "2023-01-01T12:20:00Z"}