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: -
,,
-
someinfo,,,,,,,
-
somotherinfo,,,,,,,
-
SEQ,Names,Test1,Test2,Date,Time,,
-
1,Adam,1,2,Monday,1:00 PM,,
-
2,Bob,3,4,Monday,1:00 PM,,
-
3,Charlie,5,6,Monday,1:00 PM,,
-
4,Adam,7,8,Monday,2:00 PM,,
-
5,Bob,9,10,Monday,2:00 PM,,
-
6,Charlie,11,12,Monday,2:00 PM,,
-
7,Adam,13,14,Tuesday,1:00 PM,,
-
8,Bob,15,16,Tuesday,1:00 PM,,
-
9,Charlie,17,18,Tuesday,1:00 PM,,
-
into (newfile.csv): -
Adam-Test1,Adam-Test2,Bob-Test1,Bob-Test2,Charlie-Test1,Charlie-
-
Test2,Date,Time
-
1,2,3,4,5,6,Monday,1:00 PM
-
7,8,9,10,11,12,Monday,2:00 PM
-
13,14,15,16,17,18,Tuesday,1:00 PM
-
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
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: - import csv
-
labels = ['SEQ','Names','Test1','Test2','Date','Time']
-
f = open(r"csv1.txt")
-
-
n = 4
-
# skip first 4 lines
-
for i in range(4):
-
f.next()
-
-
reader = csv.DictReader(f, labels)
-
readerDict = {}
-
-
for item in reader:
-
for key in item:
-
readerDict.setdefault(key, []).append(item[key])
-
-
f.close()
-
-
for key in labels:
-
print key, readerDict[key]
Output: - >>> SEQ ['1', '2', '3', '4', '5', '6', '7', '8', '9']
-
Names ['Adam', 'Bob', 'Charlie', 'Adam', 'Bob', 'Charlie', 'Adam', 'Bob', 'Charlie']
-
Test1 ['1', '3', '5', '7', '9', '11', '13', '15', '17']
-
Test2 ['2', '4', '6', '8', '10', '12', '14', '16', '18']
-
Date ['Monday', 'Monday', 'Monday', 'Monday', 'Monday', 'Monday', 'Tuesday', 'Tuesday', 'Tuesday']
-
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']
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
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...
|
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...
|
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
|
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...
|
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>...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
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,...
|
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...
|
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...
| |