HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

CSV import-export in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pythoncsvexportimport

Problem

I have finished a really short and small import / export script using Python 2.7, and now I would like to structure it using classes and methods where possible.

Could anybody give me some advice on what would be the best approach for this?

I haven't used any OOP because I would also like somebody to give me some best practices on how this should be done/structured.

from similarweb import ContentClient
import csv

FILE_OUT = 'similarweb/new_domains.csv'
FILE_IN = 'similarweb/domains.csv'

content_client = ContentClient("some_key")
final_result = ""

with open(FILE_IN, 'r') as csv_read_file:
    reader = csv.reader(csv_read_file)
    for i, line in enumerate(reader):
        url = ', '.join(str(e) for e in line)
        final_result += url + " " + (content_client.category(url)['Category']) + "\n"

with open(FILE_OUT, 'w') as csv_write_file:
    csv_write_file.write(final_result)

Solution

Rather than going down the OOP route, I would just split this out into a few functions:

import csv

from similarweb import ContentClient

CLIENT_KEY = 'some_key'
FILE_IN = 'similarweb/domains.csv'
FILE_OUT = 'similarweb/new_domains.csv'

def extract_data(path, client):
    """Extract the data from the specified path using the client."""
    lines = []
    with open(path) as read_file:
        for line in csv.reader(read_file):
            url = ', '.join(line)  # see note below
            lines.append(' '.join(
                (url, client.category(url)['Category'])
            ))
    return '\n'.join(lines)

def save_data(path, data):
    """Save the data to the specified path."""
    with open(path, 'w') as csv_write_file:
        csv_write_file.write(data)      

if __name__ == '__main__':
    save_data(FILE_OUT, extract_data(FILE_IN, ContentClient(CLIENT_KEY)))


Per the documentation:


Each row read from the csv file is returned as a list of strings. No automatic data type conversion is performed.

so there is no need to explicitly convert to strings.

Code Snippets

import csv

from similarweb import ContentClient

CLIENT_KEY = 'some_key'
FILE_IN = 'similarweb/domains.csv'
FILE_OUT = 'similarweb/new_domains.csv'

def extract_data(path, client):
    """Extract the data from the specified path using the client."""
    lines = []
    with open(path) as read_file:
        for line in csv.reader(read_file):
            url = ', '.join(line)  # see note below
            lines.append(' '.join(
                (url, client.category(url)['Category'])
            ))
    return '\n'.join(lines)

def save_data(path, data):
    """Save the data to the specified path."""
    with open(path, 'w') as csv_write_file:
        csv_write_file.write(data)      

if __name__ == '__main__':
    save_data(FILE_OUT, extract_data(FILE_IN, ContentClient(CLIENT_KEY)))

Context

StackExchange Code Review Q#92123, answer score: 4

Revisions (0)

No revisions yet.