37,720
社区成员
发帖
与我相关
我的任务
分享
table=fileopen.fileopen_wholedata()
tablecols=table.ncols
datarol=[]
datarol.append(table.row_values(0))
workbook=xlwt.Workbook(encoding = 'ascii')
worksheet1 = workbook.add_sheet('Sheet1')
worksheet2 = workbook.add_sheet('Sheet2')
for i in range(wholeStartTime,wholeEndTime):
# acquire the rols and write to the file
data=table.col_values(i)
datarol.append(data)
for i in range(int(wholeEndTime-wholeStartTime)):
for j in range(tablecols):
if j<255:
worksheet1.write(i,j,'1')
else :
worksheet2.write(i,j-255,'1')
pathfind()
workbook.save('C:\\John-to-Echo\\situation4.xlsx')
前一个for 循环是为了取数据,后一个是为了写数据,写完之后文件创建成功,就是无法打开,提示格式或者扩展有问题,请问是为什么path=''
def filewalk():
filepath=[]
root='C:/Echo-to-John/cut-in MF4'
for i in os.listdir(root):
root2=root+'/'+i
for j in os.listdir(root2):
if j == 'wholedata.xlsx':
filepath.append(root2)
return filepath
def pathset(fileppath):
path=fileppath
print path
def fileopen_wholedata():
try:
data=xlrd.open_workbook(path+'/wholedata.xlsx')
table=data.sheet_by_name('Sheet1')
return table
except Exception,e:
print str(e)
def p0_index_find():
p0_index=[]
table=fileopen_wholedata()
p0_index=table.col_values(1)
return p0_index
path=fileopen.filewalk()
for i in range(len(path)):
print path[i]
fileopen.pathset(path[i])
if pretreatment.pretreatment1_p0():
def pretreatment1_p0():
p0_index=fileopen.p0_index_find()
for i in range(1,len(p0_index)-1):
if p0_index[i] == 255.0 :
return False
return True
第一段代码中的两个函数就是fileopen module中的def fileopen_wholedata():
try:
data=xlrd.open_workbook('C:\\Echo-to-John\\cut-in MF4\\PJ_360_2016-02-26_14-23_0011\\wholedata.xlsx')
table=data.sheet_by_name('Sheet1')
return table
except Exception,e:
print str(e)
这样是可以的,运行正常,但是这样就不对了def fileopen_wholedata():
try:
data=xlrd.open_workbook(path)
table=data.sheet_by_name('Sheet1')
return table
except Exception,e:
print str(e)
,该怎么改?拼接路径数组代码是这样的def filewalk():
filepath=[]
root='C:\\\\Echo-to-John\\\\cut-in MF4'
for i in os.listdir(root):
root2=root+'\\\\'+i
for j in os.listdir(root2):
if j == 'wholedata.xlsx':
filepath.append(root2+'\\\\'+'wholedata.xlsx')
return filepath
def filewalk(): # 可以用os.walk
filepath=[]
root='C:\\\\Echo-to-John\\\\cut-in MF4' # 换成\\
for i in os.listdir(root):
root2=root+'\\\\'+i # 用os.path.join
for j in os.listdir(root2):
if j == 'wholedata.xlsx':
filepath.append(root2+'\\\\'+'wholedata.xlsx') # 用os.path.join
return filepath
.
2. 拼接路径用"os.path.join", 可移植性更好. 不要自己用"/"或"\".
3. 即使需要自己拼, 用os.sep, 不要用"\" 或"/".
4. python提供了遍历文件的函数os.walk.