very high-level IO functions?

Larry Bates larry.bates at websafe.com
Mon Sep 19 23:44:13 CEST 2005


It's so easy (using csv module), no need to build in.
You can wrap in a class if you want to make even easier.
Same can be done for tables from SQL database.

import csv
fp=open(r'C:\test.txt', 'r')
#
# test.txt contains:
#
# "record","value1","value2"
# "1","2","3"
# "2","4","5"
# "3","6","7"
table=csv.DictReader(fp)
for record in table:
    #
    # Record is a dictionary with keys as fieldnames
    # and values of the data in each record
    #
    print "record #=%s, value1=%s, value2=%s" % \
          (record['record'],record['value1'],record['value2'])

fp.close()

-Larry Bates


York wrote:
> Your are right, a program cannot be smarter than its programmer. However
> I need a program to parse any table-format data files offered by user. R
> offer such a function, I hope python such a function too.
> 
> -York
> 
> 
>> While it may "attempt" to recognize the types, it in fact cannot
>> be more correct than the programmer.  Example:
>>
>> data="""0X1E04 111"""
>>
>> That "looks" lile a hex and an int.  But wait.  What if it is
>> instead two strings?
>>
>> In Python you can easily write a class with a interator that can
>> read the data from the file/table and return the PROPER data types
>> as lists, tuples, or dictionaries that are easy to manipulate.
>>
>> -Larry Bates
>>
>> York wrote:
>>
>>> Hi,
>>>
>>> R language has very high-level IO functions, its read.table can read a
>>> total .csv file and recogonize the types of each column. write.table can
>>> do the reverse.
>>>
>>> R's MySQL interface has high-level functions, too, e.g. dbWriteTable can
>>>      automatically build a MySQL table and write a table of R data into
>>> it.
>>>
>>> Is there any python packages do similar things?
>>>
>>>
>>> -York



More information about the Python-list mailing list