Python Forum
Extract data csv file and write in another file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extract data csv file and write in another file
#1
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
Reply
#2
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
Reply
#3
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.
Keep it simple, stupid — kiss principle.
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation URGENT: How to plot data from text file. Trying to recreate plots from MATLAB JamieAl 4 3,472 Dec-03-2023, 06:56 AM
Last Post: Pedroski55
  Newbie here. Create an array from file data? Rayj00 2 1,193 Jan-13-2023, 01:35 PM
Last Post: perfringo
  about write file wrong (Edit directly online) CNenfan 4 2,452 Jan-29-2021, 05:32 AM
Last Post: deanhystad
  Write to file emilng 1 1,678 Nov-08-2020, 08:44 PM
Last Post: Gribouillis
  Can we store value in file if we open file in read mode? prasanthbab1234 3 2,507 Sep-26-2020, 12:10 PM
Last Post: ibreeden
  Changing the content of the table and saving the new data into another file femke_pythonquestion123 1 1,543 Sep-18-2020, 12:06 PM
Last Post: femke_pythonquestion123
  Read data from a CSV file in S3 bucket and store it in a dictionary in python Rupini 3 6,876 May-15-2020, 04:57 PM
Last Post: snippsat
  Read text file, process data and print specific output Happythankyoumoreplease 3 2,849 Feb-20-2020, 12:19 PM
Last Post: jefsummers
  Data extraction from (multiple) MS Word file(s) in python Den0st 8 7,760 Sep-19-2019, 10:07 PM
Last Post: Den0st
  help! function that checks if a file is a bed file, tips so i can code it myself lilyS 1 2,336 May-31-2019, 12:41 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020