Rich Shepard wrote:
On Tue, 22 Nov 2011, Rich Shepard wrote:
So, that clears up one issue. The malformed first three lines remain.
Could that be because I don't have the row[] properly indexed? Here
are the
first 4 lines of output:
['1', 'SW-1', '1990-10-10']||1
['1', 'SW-1', '1990-10-10']||SW-1
['1', 'SW-1', '1990-10-10']||1990-10-10
['1', 'SW-1', '1990-10-10']|Ag|0.000
The issue, I believe, is that I'm asking the writer to put the first
three
tokens in each row in a list (which does not explain why each of the first
row's leading columns is in a separate row at the top). What I want to see
(and apparently don't know how to achieve) is to have the first three rows
above not produced, then all subsequent rows looking like a reformated
row 4
above:
1|SW-1|1990-10-10|Ag|0.000
Making a few modifications to the code I suggested back in January ;)
8<-----------------------------------------------------------------------
import csv
reader = csv.reader(open("test.txt","rb"),
delimiter="|",
quotechar="'")
output = csv.writer(open("out.txt","wb"),
delimiter="|",
quotechar="'")
# records are in columnar, not row, format
all_data = list()
col_headers = reader.next()
col_header_count = 0
for cell in col_headers:
if cell:
break
col_header_count += 1
col_headers = col_headers[col_header_count:]
for row in reader:
row = list(row)
# keep fieldname separate from values
all_data.append((row[:col_header_count], row[col_header_count:]))
for row_header, row_data in all_data:
for col_header, col_data in zip(col_headers, row_data):
output.writerow(row_header + [col_header, col_data])
8<-----------------------------------------------------------------------
~Ethan~
_______________________________________________
Portland mailing list
[email protected]
http://mail.python.org/mailman/listinfo/portland