printing values for x,y,and z in columns, python
import xlrd
import os
path = 'data'
with open('', 'w') as f:
column_values=['', '', '']
for filename in os.listdir(path):
fullname = os.path.join(path, filename)
if fullname.endswith('.xls'):
wb = xlrd.open_workbook(fullname)
sh = wb.sheet_by_name(u'')
for j in range(0,sh.nrows):
for ind in range(3):
column_values[ind] += ((', ' if column_values[ind] else '') +
str(sh.cell(j,ind).value))
# more reasonable format put lists one per row, x, y and z
f.write('\n'.join(column_values)+'\n')
this script prints the values in the x values in th开发者_如何学编程e first row, y values in the second row, and the z values in the 3rd row. so how do i make it print he values in the first three columns?
Well, that's very, very basic programming. I would rewrite the script like this:
import xlrd
import os
path = 'E:\\IMRT_C\\Preanalysis\\'
with open('E:\\IMRT_C\\Working_files\\Trying_excel.csv', 'w') as f:
column_values=[[], [], []]
for filename in os.listdir(path):
fullname = os.path.join(path, filename)
if fullname.endswith('.xls'):
print('Handling %r' % filename)
wb = xlrd.open_workbook(fullname)
sh = wb.sheet_by_name(u'RawTrackingData')
for j in range(21,sh.nrows):
for i in range(3):
column_values[i] += [str(sh.cell(j, i).value))]
# more reasonable format put lists one per row, x, y and z
for i in range(len(column_values[0])):
for j in range(3):
f.write(column_values[j][i] + (", " if j < 2 else "\n"))
I haven't tested this, but I hope it works.
精彩评论