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')
此脚本打印第一行中x值的值,第二行中的y值以及第3行中的z值。那么如何让它在前三列中打印出来?
答案 0 :(得分:0)
嗯,这是非常非常基本的编程。我会像这样重写脚本:
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"))
我没有测试过这个,但我希望它有效。