473,403 Members | 2,293 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,403 software developers and data experts.

How to parse csv using DictReader?

2
Im trying to create a visualisation from radiation data from Japan, from this website: http://www.sendung.de/japan-radiation-open-data/
It is in csv format. I was told python is good for parsing csv.
Im struggling to get to grips with how to use the csv module to parse specific rows of data. I tried using DictReader but Im really not getting anywhere, total noob.

There are four column headers: station_id, datetime, sa, ra

I need to get the data for every row with a specific station_id e.g. all the data for every instance of station_id: 1070000016.

Thanks in advance for any help.
Mar 24 '11 #1
2 14086
bvdet
2,851 Expert Mod 2GB
I think this will do what you are looking for:
Expand|Select|Wrap|Line Numbers
  1. import csv
  2.  
  3. labels = ['station_id', 'datetime', 'sa', 'ra']
  4. fn = r'D:\SDS2_7.0\macro\Work In Progress\station_data_1h.csv'
  5.  
  6. station = '1070000016'
  7.  
  8. f = open(fn, 'rb')
  9.  
  10. reader = csv.DictReader(f, labels, dialect='excel-tab')
  11.  
  12. station_data = [(item["sa"],
  13.                  item["ra"],
  14.                  item["datetime"]) for item in reader if \
  15.                 item['station_id'] == station]
  16.  
  17. f.close()
  18.  
  19. print "Data for station %s" % (station)
  20. print "\n".join(["%s\t%s\t%s" % (item) for item in station_data])
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> Data for station 1070000016
  2. -999    -888    2011-03-24 20:10:00
  3. -999    -888    2011-03-24 20:20:00
  4. -999    -888    2011-03-24 20:30:00
  5. -999    -888    2011-03-24 20:40:00
  6. -999    -999    2011-03-24 20:50:00
  7. >>> 
Mar 24 '11 #2
Ben D
2
That was surprisingly straight forward - thank you very much!
Apr 5 '11 #3

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

Similar topics

0
by: precious | last post by:
i'm trying to split my xml configuration file to files implementing the parsing using xerces in java (DOMparser). my project uses a "fromXML" parsing design and here's what i tried in the node...
2
by: KB | last post by:
Hi folks, I'm trying to adopt microsoft-specific code to mozilla. I have an xml file which I can't figure out how to parse using the XMLDocument object: <?xml version="1.0" encoding="utf-16"...
24
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and...
10
by: Reiner Merz | last post by:
Hi, I'm looking for advice on how to parse a timestamp string according to the ISO 8601 specification. For those unfamiliar with the standard, here's an example: 2003-09-09T23:00:00Z...
7
by: memememe | last post by:
I need to be able to parse both of these dates with the same method, and return them as a DateTime object. Is there any methods that would do this blindly or do I need to provide the format? Feb...
7
by: David Lozzi | last post by:
I need to support multiple keyword search, not boolean searchs, just simple searches, i.e. "marhsall ma". How is this done? Do I send the entire search string to the Proc? How do I deal with it...
0
by: sumelong | last post by:
Hi all, i have an xsl with params to be parse using c#. I then used xslArgumentList and XpathNavigator Classes. I tried the following: 1-trans.transform(doc,params,writer,null). I had am empty...
9
by: seberino | last post by:
I'm a compiler newbie and curious if Python grammar is able to be parsed by a recursive descent parser or if it requires a more powerful algorithm. Chris
1
by: digidave | last post by:
I am keenly aware that my coding skills are extremely noob but please indulge me a second.. Take a look at these queries.. $sql = "SELECT DISTINCT year FROM _current_floats_config WHERE active =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.