[Tutor] Reading .csv data vs. reading an array
Peter Otten
__peter__ at web.de
Tue Jul 16 17:13:30 EDT 2019
Chip Wachob wrote:
> I tried it anyhow, with this being an example of my source data:
>
> "Record Length",2000002,"Points",-0.005640001706,1.6363
> "Sample Interval",5e-09,s,-0.005639996706,1.65291
> "Trigger Point",1128000,"Samples",-0.005639991706,1.65291
> "Trigger Time",0.341197,s,-0.005639986706,1.60309
> ,,,-0.005639981706,1.60309
> "Horizontal Offset",-0.00564,s,-0.005639976706,1.6363
> ,,,-0.005639971706,1.65291
> ,,,-0.005639966706,1.65291
> ,,,-0.005639961706,1.6363
> .
> .
> .
>
> Note that I want the items in the third and fourth column of the csv file
> for my time and voltage.
>
> When I tried to use the unpack, they all came over as strings. I can't
> seem to convert them selectively..
Try wrapping the reader like this:
$ cat custom_reader.py
import csv
import io
data = """\
"Record Length",2000002,"Points",-0.005640001706,1.6363
"Sample Interval",5e-09,s,-0.005639996706,1.65291
"Trigger Point",1128000,"Samples",-0.005639991706,1.65291
"Trigger Time",0.341197,s,-0.005639986706,1.60309
,,,-0.005639981706,1.60309
"Horizontal Offset",-0.00564,s,-0.005639976706,1.6363
,,,-0.005639971706,1.65291
,,,-0.005639966706,1.65291
,,,-0.005639961706,1.6363
"""
def maybe_float(s):
try:
return float(s)
except ValueError:
return s
def myreader(*args, **kw):
reader = csv.reader(*args, **kw)
for row in reader:
yield [maybe_float(field) for field in row]
for row in myreader(io.StringIO(data)):
print(row)
$ python3 custom_reader.py
['Record Length', 2000002.0, 'Points', -0.005640001706, 1.6363]
['Sample Interval', 5e-09, 's', -0.005639996706, 1.65291]
['Trigger Point', 1128000.0, 'Samples', -0.005639991706, 1.65291]
['Trigger Time', 0.341197, 's', -0.005639986706, 1.60309]
['', '', '', -0.005639981706, 1.60309]
['Horizontal Offset', -0.00564, 's', -0.005639976706, 1.6363]
['', '', '', -0.005639971706, 1.65291]
['', '', '', -0.005639966706, 1.65291]
['', '', '', -0.005639961706, 1.6363]
If you find that performance suffers more than you are willing to accept
here's an alternative implementation of maybe_float() that may be faster for
some inputs:
def maybe_float(s):
if s and s[:1] in " 0123456789+-":
try:
return float(s)
except ValueError:
return s
return s
More information about the Tutor
mailing list