Skip to main content
Fixed error in code
Source Link
BlackJack
  • 4.7k
  • 1
  • 21
  • 26

Here is code to write a CSV file with the csv module from the standard library. If the first column contains the status and the following columns the senders:

#!/usr/bin/env python3
import csv

import json_lines


def main():
    with json_lines.open("specifications.jsonl") as reader:
        with open("senderv1.csv", "w", encoding="utf8") as csv_file:
            writer = csv.writer(csv_file, delimiter="\t")
            for item in reader:
                row = [item["status"]]
                if "sender_id" in item:
                    row.append(item["sender_id"])
                elif "senders" in item:
                    row.extend(sender["id"] for sender in item["senders"])
                else:
                    raise ValueError("item with no sender information")
                writer.writerow(row)


if __name__ == "__main__":
    main()

To have the same information spread across different columns isn't really good, but putting more than one value into a single cell isn't good either. CSV is best suited for two dimensional tabular data. Maybe you want JSON (Lines) for the result too‽

Here is code to write a CSV file with the csv module from the standard library. If the first column contains the status and the following columns the senders:

#!/usr/bin/env python3
import csv

import json_lines


def main():
    with json_lines.open("specifications.jsonl") as reader:
        with open("senderv1.csv", "w", encoding="utf8") as csv_file:
            writer = csv.writer(csv_file, delimiter="\t")
            for item in reader:
                row = [item["status"]]
                if "sender_id" in item:
                    row.append(item["sender_id"])
                elif "senders" in item:
                    row.extend(sender["id"] for sender in item["senders"])
                else:
                    raise ValueError("item with no sender information")
            writer.writerow(row)


if __name__ == "__main__":
    main()

To have the same information spread across different columns isn't really good, but putting more than one value into a single cell isn't good either. CSV is best suited for two dimensional tabular data. Maybe you want JSON (Lines) for the result too‽

Here is code to write a CSV file with the csv module from the standard library. If the first column contains the status and the following columns the senders:

#!/usr/bin/env python3
import csv

import json_lines


def main():
    with json_lines.open("specifications.jsonl") as reader:
        with open("senderv1.csv", "w", encoding="utf8") as csv_file:
            writer = csv.writer(csv_file, delimiter="\t")
            for item in reader:
                row = [item["status"]]
                if "sender_id" in item:
                    row.append(item["sender_id"])
                elif "senders" in item:
                    row.extend(sender["id"] for sender in item["senders"])
                else:
                    raise ValueError("item with no sender information")
                writer.writerow(row)


if __name__ == "__main__":
    main()

To have the same information spread across different columns isn't really good, but putting more than one value into a single cell isn't good either. CSV is best suited for two dimensional tabular data. Maybe you want JSON (Lines) for the result too‽

Source Link
BlackJack
  • 4.7k
  • 1
  • 21
  • 26

Here is code to write a CSV file with the csv module from the standard library. If the first column contains the status and the following columns the senders:

#!/usr/bin/env python3
import csv

import json_lines


def main():
    with json_lines.open("specifications.jsonl") as reader:
        with open("senderv1.csv", "w", encoding="utf8") as csv_file:
            writer = csv.writer(csv_file, delimiter="\t")
            for item in reader:
                row = [item["status"]]
                if "sender_id" in item:
                    row.append(item["sender_id"])
                elif "senders" in item:
                    row.extend(sender["id"] for sender in item["senders"])
                else:
                    raise ValueError("item with no sender information")
            writer.writerow(row)


if __name__ == "__main__":
    main()

To have the same information spread across different columns isn't really good, but putting more than one value into a single cell isn't good either. CSV is best suited for two dimensional tabular data. Maybe you want JSON (Lines) for the result too‽