473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

intermediate python csv reader/writer question from a beginner

anything related to csv, I usually use VB within excel to manipulate
the data, nonetheless, i finally got the courage to take a dive into
python. i have viewed a lot of googled csv tutorials, but none of
them address everything i need. Nonetheless, I was wondering if
someone can help me manipulate the sample csv (sample.csv) I have
generated:
Expand|Select|Wrap|Line Numbers
  1. ,,
  2. someinfo,,,,,,,
  3. somotherinfo,,,,,,,
  4. SEQ,Names,Test1,Test2,Date,Time,,
  5. 1,Adam,1,2,Monday,1:00 PM,,
  6. 2,Bob,3,4,Monday,1:00 PM,,
  7. 3,Charlie,5,6,Monday,1:00 PM,,
  8. 4,Adam,7,8,Monday,2:00 PM,,
  9. 5,Bob,9,10,Monday,2:00 PM,,
  10. 6,Charlie,11,12,Monday,2:00 PM,,
  11. 7,Adam,13,14,Tuesday,1:00 PM,,
  12. 8,Bob,15,16,Tuesday,1:00 PM,,
  13. 9,Charlie,17,18,Tuesday,1:00 PM,,
  14.  
into (newfile.csv):
Expand|Select|Wrap|Line Numbers
  1. Adam-Test1,Adam-Test2,Bob-Test1,Bob-Test2,Charlie-Test1,Charlie-
  2. Test2,Date,Time
  3. 1,2,3,4,5,6,Monday,1:00 PM
  4. 7,8,9,10,11,12,Monday,2:00 PM
  5. 13,14,15,16,17,18,Tuesday,1:00 PM
  6.  
note:
1. the true header doesn't start line 4 (if this is the case would i
have to use "split"?)
2. if there were SEQ#10-12, or 13-15, it would still be Adam, Bob,
Charlie, but with different Test1/Test2/Date/Time
Feb 23 '09 #1
1 3098
bvdet
2,851 Expert Mod 2GB
The key is to get the data into a dictionary that can be manipulated for your purpose. Note that the following code keeps the data in order:
Expand|Select|Wrap|Line Numbers
  1. import csv
  2. labels = ['SEQ','Names','Test1','Test2','Date','Time']
  3. f = open(r"csv1.txt")
  4.  
  5. n = 4
  6. # skip first 4 lines
  7. for i in range(4):
  8.     f.next()
  9.  
  10. reader = csv.DictReader(f, labels)
  11. readerDict = {}
  12.  
  13. for item in reader:
  14.     for key in item:
  15.         readerDict.setdefault(key, []).append(item[key])
  16.  
  17. f.close()
  18.  
  19. for key in labels:
  20.     print key, readerDict[key]
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> SEQ ['1', '2', '3', '4', '5', '6', '7', '8', '9']
  2. Names ['Adam', 'Bob', 'Charlie', 'Adam', 'Bob', 'Charlie', 'Adam', 'Bob', 'Charlie']
  3. Test1 ['1', '3', '5', '7', '9', '11', '13', '15', '17']
  4. Test2 ['2', '4', '6', '8', '10', '12', '14', '16', '18']
  5. Date ['Monday', 'Monday', 'Monday', 'Monday', 'Monday', 'Monday', 'Tuesday', 'Tuesday', 'Tuesday']
  6. Time ['1:00 PM', '1:00 PM', '1:00 PM', '2:00 PM', '2:00 PM', '2:00 PM', '1:00 PM', '1:00 PM', '1:00 PM']
  7. >>> 
Feb 24 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: srijit | last post by:
Hello, Here is an example code of xml writer in Python+PythonNet, Ironpython and Boo. The codes look very similar. Regards, Srijit Python + PythonNet: import CLR
2
by: googleboy | last post by:
Hi. I am trying to write out a csv file with | instead of comma, because I have a field that may have many commas in it. I read in a csv file, sort it, and want to write it out again. I read...
2
by: Lumpierbritches | last post by:
Thank you in advance for any and all assistance. It is greatly appreciated. Has anyone used Access to keep track with reader/writer magnetic encoders? I have purchased a Fargo Printer that does...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
14
by: Rochester | last post by:
Hi, I just found out that the general open file mechanism doesn't work for named pipes (fifo). Say I wrote something like this and it simply hangs python: #!/usr/bin/python import os
6
by: roblugt | last post by:
I have what I imagine is a well-known .Net networking problem, but even though I've Googled for some time I've not yet come across a thread where this has been fully explained... There is a...
3
by: Matt Nunnally | last post by:
I'm using XmlReader to read an XMl String such as this one: <XML_REQUEST> <USER_ID>Admin</USER_ID> <USER_PW>Test123%</USER_PW> <COMMAND>1</COMMAND> <PRESENT_FLAG>1</PRESENT_FLAG>...
9
by: Mike P | last post by:
Hi All, I want to read in a CSV file, but then write out a new CSV file from a given line.. I'm using the CSV reader and have the the line where i want to start writing the new file from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.