# CSV import-export in Python

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)


## 2 Answers

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)))


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.

• This is exactly what I was trying to accomplish ! Thanks @jonrsharpe for this fast turnaround and the explanations. I have in a csv file some words(each on each line - so one column) and when i export it there will be two columns. I just have one question: is there a way to separate these columns by tabs ? – Cajuu' May 29 '15 at 9:41
• @Alexander read the CSV documentation I've already linked to, which explains how to set the delimiter - a comma is the default, but you can specify an alternative if you want. – jonrsharpe May 29 '15 at 9:42
• I tried before and also now and there's just a simple space. for line in csv.reader(read_file, delimiter=','): won't work – Cajuu' May 29 '15 at 9:50
• @Alexander I'm not sure what the problem is - I thought you wanted to change the delimiter for the output file? You should cut down to a minimal example and take the question to Stack Overflow if you really can't get the code to work, but I suggest you spend more time with the documentation and some experimentation first. – jonrsharpe May 29 '15 at 9:52
• The problem is that I've tried in many ways to do it but didn't succeed – Cajuu' May 29 '15 at 10:41

Writing classes is not an aim in itself, it is just a tool. Sometimes it's useful, sometimes it's not. I recommend the talk Stop Writing Classes.

Your code looks OK as it is, even if a few things can be improved. For instance, I'm not sure you use the value i so you probably don't need enumerate.

• you're right, I've changed that. I missed it because I used it at some point. More, I would like to structure my code so that it would be more readable.More, I'd also like to have this done for learning purposes on my own example. I am asking for some OOP for my code because it will probably become bigger and bigger. Would you give it a try with a structure and some explanations along it ? – Cajuu' May 29 '15 at 8:51
• @Alexander OOP isn't the only way to structure "bigger and bigger"; it's important if you need to combine state and behaviour, but otherwise a sensible module of functions would be more flexible. – jonrsharpe May 29 '15 at 9:13
• That's what I am looking for @jonrsharpe . I was thinking of putting the hole thing in a class something(): using some def methods(): and a main(). But I don't have any experience and I want to learn the best way of doing it – Cajuu' May 29 '15 at 9:20