简体   繁体   中英

Csv file into seperate files

I need to separate few csv files to incoming and outgoing traffic based on column values. This code doesn't give any output nor any error.

if source == ac:37:43:9b:92:24 && Receiver address == 8c:15:c7:3a:d0:1a then those rows need to get written to .out.csv files.

if Transmitter address == 8c:15:c7:3a:d0:1a && Destination== ac:37:43:9b:92:24 then those rows need to get written into .in.csv files.

import csv
import os
import subprocess

startdir = '.'   
outdir = '.'
suffix = '.csv'

def decode_to_file(cmd, in_file, new_suffix):
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    filename = outdir + '/' + in_file[len(startdir):-len(suffix)] + new_suffix
    os.makedirs(os.path.dirname(filename), exist_ok=True)
    csv_writer = csv.writer(open(filename, 'w'))
    for line_bytes in proc.stdout:
        line_str = line_bytes.decode('utf-8')
        csv_writer.writerow(line_str.strip().split(','))

for root, dirs, files in os.walk(startdir):
    for name in files:
        if not name.endswith(suffix):
            continue
        in_file = os.path.join(root, name)

        decode_to_file(
            cmd= [source== "ac:37:43:9b:92:24" && Receiver address== "8c:15:c7:3a:d0:1a"],
            in_file=in_file,
            new_suffix='.out.csv'
        )
        decode_to_file(
            cmd= [Transmitter address == "8c:15:c7:3a:d0:1a" && Destination== "ac:37:43:9b:92:24"],
            in_file=in_file,
            new_suffix='.in.csv'
        )

在此处输入图片说明

import csv
from pathlib import Path

startdir = Path(".")
outputdir = startdir / "filtered"


def write_csv(filename, rows, fieldnames):
    # if there's nothing to write, don't write anything
    if not rows:
        return

    filename.parent.mkdir(parents=True, exist_ok=True)
    with open(filename, "w") as f:
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(rows)


for input_filename in startdir.glob("**/*.csv"):
    with open(input_filename) as input_csv:
        inrows = []
        outrows = []

        reader = csv.DictReader(input_csv)
        for row in reader:
            if (
                row["Source address"] == "ac:37:43:9b:92:24"
                and row["Receiver address"] == "8c:15:c7:3a:d0:1a"
            ):
                outrows.append(row)

            if (
                row["Transmitter address"] == "8c:15:c7:3a:d0:1a"
                and row["Destination address"] == "ac:37:43:9b:92:24"
            ):
                inrows.append(row)

        output_filename = outputdir / input_filename.relative_to(startdir)

        write_csv(output_filename.with_suffix(".out.csv"), outrows, reader.fieldnames)
        write_csv(output_filename.with_suffix(".in.csv"), inrows, reader.fieldnames)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM