Vishal Mahajan
Vishal Mahajan

Reputation: 84

Output content in CSV format python

My script will process few files a different path, I want to write those output in CSV format in python.

For example:

%> script_name <file_name>

In every file, I have different options to be checked.

For example : file1:

Best_friend : Riky
Mutual_friend : Anuj
Family_friend : Jamie

For example : file2:

Best_friend : Anjelina
Mutual_friend : Mythe

For example : file3:

Best_friend : Mahira
Mutual_friend : Shyna
Dear_frind : Kisty

I want to create CSV in the format

File,Best_friend, Mutual_friend
File1,Riky,Anuj
File2,Anjelina,Mythe
File3,Mahira,shyna

Please help

Upvotes: 0

Views: 118

Answers (2)

ajay_t
ajay_t

Reputation: 2385

Using csv dictReader/dictWriter are more efficient way of handling the csv files.

Hope this will solve your problem:

import sys
import csv
import copy

def create_csv(files):
    headers=  ['File', 'Best Friend', 'Mutual Friend']
    list1 = []
    for file in files:
        with open(file,'r') as file_obj:
            dict_temp = {}
            dict_temp['File'] = file
            for line in file_obj:
                if line.split(':')[0] == 'Best_friend ':
                    dict_temp['Best Friend'] = line.split(':')[1].strip()
                if line.split(':')[0] == 'Mutual_friend ':
                    dict_temp['Mutual Friend'] = line.split(':')[1].strip()
        list1.append(dict_temp)
    print list1

    csv_result = open('result.csv','wb')
    writer = csv.DictWriter(csv_result, delimiter=',', fieldnames=headers, quoting=csv.QUOTE_NONE)
    writer.writeheader()
    for entry in list1:
        writer.writerow(entry)
    csv_result.close()

if __name__ == "__main__":
    create_csv(sys.argv[1:])

You can add/remove the columns in csv just by adding it in dictionary with appropriate key.

Upvotes: 0

Francis Colas
Francis Colas

Reputation: 3647

Well, there's several things to your question. You want to get passed several files, read some values in each of them, then output the values into csv file. It helps if you decompose your problem into several successive steps.

First, you need to know how to read the best and mutual friend in a given file. You can do that in a function:

def get_best_mutual(filename):
    # some code
    return (best_friend, mutual_friend)

Then, you can just iterate over all your files to write the values while you collect them:

for filename in list_of_filenames:
    best_friend, mutual_friend = get_best_mutual(filename)
    # write filename, best_friend, mutual_friend in output file

Writing into the file should be easy, I'll not go into the details. The problem might be to actually get the values from the input files.

When you read a text file, you typically read it line by line. Then you can just look at your line to decide what to do: if it defines either best or mutual friend, save the definition, otherwise do nothing.

Concretely, it might look like:

def get_best_mutual(filename):
    for line in open(filename): # read each line of the file
        key, value = line.split(':', 1) # split the line along the first :
        if key.startswith('Best'):
            best_friend = value
        if key.startswith('Mutual'):
            mutual_friend = value
    return (best_friend, mutual_friend)

Obviously, you'd have to protect a bit more the code, in case for example the line doesn't have a ':' in it, and you might also notice that the value starts with a space and ends with a '\n': you can use value.strip() to solve that. Same for the key, if a line starts with a space the code above will not recognize it.

You also need to decide what to do if a file doesn't have a best_friend, for example.

Upvotes: 1

Related Questions