472,791 Members | 1,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 software developers and data experts.

Seperating CSV rows into new, seperate files

Hi,
I've searched the group and need more information and guidance on this
issue I need to resolve next week. I work for the local school system
and I am working on a way to parse a CSV file of class lists from MS
Excel. It is a ~2MB file with all teachers and all their classes in
it. I thought I would use Python to do this since many people I know
tell me how great it is. The real sticking point is there are
duplicates of each teacher's classes embedded in the file (don't ask
why). I want to break up the source file into the seperate class
files without duplication and use the teachers name, coursecode and
section in the filename.

Example Data:
Last Name First Name Grade Period Teacher Name
SMITH JOHN 8 1 JONES SALLY

Student ID Course Code Course Title
12345678 1234 ALGEBRA I

Course Section Session
1 0
The course code and section number are the same in the duplicate
classes. The difference is that the session number changes. I'm
thinking that I will need to somehow iterate through the lines and
break the class when the course code changes and/or section change
(course codes can be the same but section numbers change with each
class). Then I have to monitor the session number to see if there is
a duplicate course code and section but a different session number
would indicate that the class is a duplicate. Essentially, the session
number will be 0, 1, or 2 for a total of three lists of the same
class. Hopefully that makes sense. Again don't ask why the data is
like this.

My humble attempts have not been very successful. I've been trying
the csv modules to read the file but I am not sure how to procede
after that.

Any guidance is greatly appreciated.
Jul 18 '05 #1
5 2292
TonyB wrote:
I've searched the group and need more information and guidance on this
issue I need to resolve next week. I work for the local school system
and I am working on a way to parse a CSV file of class lists from MS
Excel. It is a ~2MB file with all teachers and all their classes in
it. I thought I would use Python to do this since many people I know
tell me how great it is. The real sticking point is there are
duplicates of each teacher's classes embedded in the file (don't ask
why). I want to break up the source file into the seperate class
files without duplication and use the teachers name, coursecode and
section in the filename.

Example Data:
Last Name First Name Grade Period Teacher Name
SMITH JOHN 8 1 JONES SALLY

Student ID Course Code Course Title
12345678 1234 ALGEBRA I

Course Section Session
1 0
The course code and section number are the same in the duplicate
classes. The difference is that the session number changes. I'm
thinking that I will need to somehow iterate through the lines and
break the class when the course code changes and/or section change
(course codes can be the same but section numbers change with each
class). Then I have to monitor the session number to see if there is
a duplicate course code and section but a different session number
would indicate that the class is a duplicate. Essentially, the session
number will be 0, 1, or 2 for a total of three lists of the same
class. Hopefully that makes sense. Again don't ask why the data is
like this.

My humble attempts have not been very successful. I've been trying
the csv modules to read the file but I am not sure how to procede
after that.

Any guidance is greatly appreciated.


From the above I think you can use the (student id, course code, course
section) tuple as the unique key when reading the data via the cvs module
into a dictionary instead of a list. (For larger data you would need a
database - the humble MS Access could have saved you from the trouble with
duplicates in the first place, btw.)

#untested
STUDENT_ID = 5
COURSE_CODE = 6
COURSE_SECTION = 8
data = {}
for row in csv.reader(instream):
# you could do more processing here,
# e. g. with nested course/student
# dictionaries, but let's keep it simple
data[row[STUDENT_ID], row[COURSE_CODE], row[COURSE_SECTION]] = row

Subsequent sessions will then just overwrite data already in the dictionary.
Note that you are throwing away some information as I am pretty sure (don't
ask why :-) that the duplicates will not be completely identical.

You can split this raw data into classes like so:

#untested
klasses = {}
for row in data.values():
key = row[COURSE_CODE], row[COURSE_SECTION]
if key in klasses:
klasses[key].append(row)
else:
klasses[key] = [row]

If you feel comfortable with (Python) classes you can make the dictionary
values instances of a Row class that lets you access attributes by name, e.
g. row.studentID instead of row[STUDENT_ID].

If these hints aren't sufficient to get you started it would help if you
showed the code you already have to avoid duplicate effort.

Peter

PS: Who am I to talk about orthography, but it's "separate".
Jul 18 '05 #2
Hi TobyB,
Hi,
I've searched the group and need more information and guidance on this
issue I need to resolve next week. I work for the local school system
and I am working on a way to parse a CSV file of class lists from MS
Excel. It is a ~2MB file with all teachers and all their classes in
it. I thought I would use Python to do this since many people I know
tell me how great it is.
Pyhton is a good choice for treating texts.
The real sticking point is there are
duplicates of each teacher's classes embedded in the file (don't ask
why). I want to break up the source file into the seperate class
files without duplication and use the teachers name, coursecode and
section in the filename.

Example Data:
Last Name First Name Grade Period Teacher Name
SMITH JOHN 8 1 JONES SALLY

Student ID Course Code Course Title
12345678 1234 ALGEBRA I

Course Section Session
1 0
The course code and section number are the same in the duplicate
classes. The difference is that the session number changes. I'm
thinking that I will need to somehow iterate through the lines and
break the class when the course code changes and/or section change
(course codes can be the same but section numbers change with each
class). Then I have to monitor the session number to see if there is
a duplicate course code and section but a different session number
would indicate that the class is a duplicate. Essentially, the session
number will be 0, 1, or 2 for a total of three lists of the same
class. Hopefully that makes sense. Again don't ask why the data is
like this.

My humble attempts have not been very successful. I've been trying
the csv modules to read the file but I am not sure how to procede
after that.

Any guidance is greatly appreciated.


Use Python lists and dictionaries.

Best regards,

Maik Röder

Jul 18 '05 #3
Hi TobyB,
Hi,
I've searched the group and need more information and guidance on this
issue I need to resolve next week. I work for the local school system
and I am working on a way to parse a CSV file of class lists from MS
Excel. It is a ~2MB file with all teachers and all their classes in
it. I thought I would use Python to do this since many people I know
tell me how great it is.
Pyhton is a good choice for treating texts.
The real sticking point is there are
duplicates of each teacher's classes embedded in the file (don't ask
why). I want to break up the source file into the seperate class
files without duplication and use the teachers name, coursecode and
section in the filename.

Example Data:
Last Name First Name Grade Period Teacher Name
SMITH JOHN 8 1 JONES SALLY

Student ID Course Code Course Title
12345678 1234 ALGEBRA I

Course Section Session
1 0
The course code and section number are the same in the duplicate
classes. The difference is that the session number changes. I'm
thinking that I will need to somehow iterate through the lines and
break the class when the course code changes and/or section change
(course codes can be the same but section numbers change with each
class). Then I have to monitor the session number to see if there is
a duplicate course code and section but a different session number
would indicate that the class is a duplicate. Essentially, the session
number will be 0, 1, or 2 for a total of three lists of the same
class. Hopefully that makes sense. Again don't ask why the data is
like this.

My humble attempts have not been very successful. I've been trying
the csv modules to read the file but I am not sure how to procede
after that.

Any guidance is greatly appreciated.


Use Python lists and dictionaries.

Best regards,

Maik Röder
Jul 18 '05 #4
OK,
With some off post help I was able to come up with this little ditty
of a script but it still has one more problem. When I run the script
it creates new files for each specific class. While talking through
the logic with a friend, I realized that the gradebook software that
will import the list will ignore duplicate rows. Not the best
solution but I need it to work this week. In any case, it appears
that the new files have some sort of additional line terminator that
creates a blank row in the outputted file making the importing of the
list impossible. This is based on the fact that when I try to open
the new files in MS Excel, they have blank rows after each row of
data. If I open them in a text editor such as Notepad, they look
identical to the source file (which can be imported to the gradbook
software).

Is there a way to SEE the newline terminators?
How can I use the csv.writer() to make sure there are no extra lines?

My Script:
**************************************
import csv

TEACHER_NAME = 4
COURSE_TITLE = 9
COURSE_NUMBER = 8
SECTION = 10

# Open the input file and read the file to memory (may be too big a
file? ~ 2MB?).
reader = csv.reader(file("sample.csv"))

def createFilename(teacher,title,number,section):
'''
We need to remove spaces from the fields for better formatting of
the file name
'''
import re
regex = re.compile('[^.A-Za-z0-9-_]')
filename = '%s_%s_%s_section%s.txt' % (teacher, title, number,
section)
return regex.sub('_', filename)

for row in reader:
filename = createFilename(row[TEACHER_NAME], row[COURSE_TITLE],
row[COURSE_NUMBER],

row[SECTION])
fileobj = file(filename, 'a')
newfile = csv.writer(fileobj)
newfile.writerow(row)
fileobj.close()
**************************************

Thanks to those who have helped!

Maik Röder <ro****@berg.net> wrote in message news:<ma**************************************@pyt hon.org>...
Hi TobyB,
Hi,
I've searched the group and need more information and guidance on this
issue I need to resolve next week. I work for the local school system
and I am working on a way to parse a CSV file of class lists from MS
Excel. It is a ~2MB file with all teachers and all their classes in
it. I thought I would use Python to do this since many people I know
tell me how great it is.


Pyhton is a good choice for treating texts.
The real sticking point is there are
duplicates of each teacher's classes embedded in the file (don't ask
why). I want to break up the source file into the seperate class
files without duplication and use the teachers name, coursecode and
section in the filename.

Example Data:
Last Name First Name Grade Period Teacher Name
SMITH JOHN 8 1 JONES SALLY

Student ID Course Code Course Title
12345678 1234 ALGEBRA I

Course Section Session
1 0
The course code and section number are the same in the duplicate
classes. The difference is that the session number changes. I'm
thinking that I will need to somehow iterate through the lines and
break the class when the course code changes and/or section change
(course codes can be the same but section numbers change with each
class). Then I have to monitor the session number to see if there is
a duplicate course code and section but a different session number
would indicate that the class is a duplicate. Essentially, the session
number will be 0, 1, or 2 for a total of three lists of the same
class. Hopefully that makes sense. Again don't ask why the data is
like this.

My humble attempts have not been very successful. I've been trying
the csv modules to read the file but I am not sure how to procede
after that.

Any guidance is greatly appreciated.


Use Python lists and dictionaries.

Best regards,

Maik Röder

Jul 18 '05 #5
TonyB wrote:
In any case, it appears
that the new files have some sort of additional line terminator that
creates a blank row in the outputted file making the importing of the
list impossible.
When writing to a file using csv.writer on Windows open the file in
binary format.
row[SECTION])
fileobj = file(filename, 'a')
newfile = csv.writer(fileobj)
newfile.writerow(row)
fileobj.close()

so
fileobj = file(filename, 'a')
becomes
fileobj = file(filename, 'ab')

That should fix it.

Waldek
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: vool | last post by:
Hi All Can anyone help with this please. I need a way of putting say 10 to 20 bullet points in one table field in an Access database - say seperate them with a special character, then build a...
5
by: Tyler Smith Watu | last post by:
I will like to seperate the contents of sp_dbfixedrolepermission into groups by the fixedrole (With the LUXURY of a heading).Is there a any short way or straight forward command to do that? ...
6
by: Jo K. | last post by:
I have a bounded field called Name. It's used to type in the full name of a customer (intentionally set up that way instead of seperating the first and last names) I would like to be able to...
0
by: AnkitAsDeveloper [Ankit] | last post by:
As all we know, in order to remove cyclic includes in C++ we seperate the declarations and definitions of classs and it's member in two files Header (*.h) and source files (*.cpp). This is not a...
3
by: roy_ware | last post by:
I am reading 4 variable length fields from a text file via StreamReader - a truly slick tool - but was wondering what was the best way to seperate the read record into its 4 fields. I've modifed...
2
by: Joe | last post by:
Hello All: I am writing to ask for your opinions. I have a colleague who combines his code with the markup used to display the code (reckoning back to classic ASP). Here's an example of a...
0
by: B.N.Prabhu | last post by:
Hi, I have a DataTable with several rows. Its having 20 Columns. when i click the Insert button then i have to check the Database Rows. Whether these new rows are already available in the...
10
by: Danigan | last post by:
It's been about 4 years since I did much in C++. To see if I remembered anything, I made a new project, made a header file containing a class, included the header file from my main file, used...
3
by: Andrew G | last post by:
Hi, am writing c# web applications and am trying to now seperate my code into seperate DLLs. Mainly so that the code is easier to manage and I can more easily 'plug in' seperatepieces of code...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.