another question
Skip Montanaro
skip at pobox.com
Thu Jun 5 20:53:17 EDT 2003
Mark> the file has spaces between the elements but the delimiter that
Mark> really separates them is a "|".
...
Mark> the problem is that i don't know how to get rid of the "|"
Mark> characters so these show up in the output.
Python 2.3 (currently in beta test) has a csv module. You should be able to
adapt it easily to read your data. Not having seen your data, I will take a
stab at it:
import csv
class dialect(csv.dialect):
delimiter = '|'
skipinitialspace = True
quotechar = '"'
doublequote = True
reader = csv.reader(file("xxx.dat"), dialect=dialect)
for row in reader:
print row
You can also use the module's Sniffer class to figure out what the
properties of the data are:
import csv
dialect = csv.Sniffer(file("xxx.dat").read(8192),
delimiters=["|"])
reader = csv.reader(file("xxx.dat"), dialect=dialect)
for row in reader:
print row
If you have field names you can use the DictReader class to return
dictionaries instead of tuples:
import csv
dialect = csv.Sniffer(file("xxx.dat").read(8192),
delimiters=["|"])
reader = csv.DictReader(file("xxx.dat"),
fieldnames=("f1","f2",...),
dialect=dialect)
for row in reader:
print row["f1"], row["f2"]
Skip
More information about the Python-list
mailing list