Python Forum
Extract data csv file and write in another file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Extract data csv file and write in another file (/thread-879.html)



Extract data csv file and write in another file - alexgrand - Nov-11-2016

I have to write a Python application to read a CSV input file (appsUsage.csv) and generate an output file counting the frequency of the apps usage per user. The first column of the input file contains user id (user_id) and the second column contains the name of the app (app_name). From appsUsage.csv, I have to count the frequency of apps usage on individual user basis to generate app_Rank, and transform to a new data set named appsIndividualUsage.csv with the following columns: user_id; app_name; app_Rank.
For example if appsUsage.csv has following events
a facebook a facebook a linkedin b google b yahoo b yahoo
Then appsIndividualUsage.csv would be:
a facebook 2; a linkedin 1; b google 1; b yahoo 2;
Here is my code that I am struggling with :
>>>import csv with open(‘C:\\Users\\anne\\Desktop\\appsUsage.csv’, mode='r') as f_in, open(‘C:\\Users\\anne\\Desktop\\appsIndividualUsage.csv’, mode='w', newline='') as f_out: 
f_reader = csv.reader(f_in, dialect=csv.excel_tab) 
f_writer = csv.writer(f_out, dialect=csv.excel_tab) 
for line in reader: 
if line is equal then write it in writer and add a row of frequency
i totally don't know how to write the last line of my code Confused


If someone can help  Smile


Thank you


RE: Extract data csv file and write in another file - Larz60+ - Nov-11-2016

Right off the bat, if you try to run the very first line
you will get
Error:
SyntaxError: invalid syntax
I'd fix that first


RE: Extract data csv file and write in another file - JChris - Nov-14-2016

I would first use a source code editor instead of terminal/IDLE, no need for fancy IDEs as of now (Notepad++ is simple and good enough in your case). Working directly on terminal/IDLE isn't the best approach, and this seems what you're doing, as I can guess from your '>>>'. By using a source code editor you have more flexibility to edit and inspect the code as a whole.


RE: Extract data csv file and write in another file - snippsat - Nov-14-2016

In IDLE there are two modes,you always start in interactive shell.
To write code File -- > New File and run it with Run module(F5).

You can test with your code that i have clean up.
Added print(line)  if it work you see output.
Then figurere out columns.
import csv

app_in = 'C:\\Users\\anne\\Desktop\\appsUsage.csv'
app_out = 'C:\\Users\\anne\\Desktop\\appsIndividualUsage.csv'

with open(app_in) as f_in, open(app_out, 'w', newline='') as f_out:
    f_reader = csv.reader(f_in, dialect=csv.excel_tab)
    f_writer = csv.writer(f_out, dialect=csv.excel_tab)
    for line in reader:
        print(line)
        #if line is equal then write it in writer and add a row of frequency