- 积分
- 48
- 注册时间
- 2010-7-6
- 仿真币
-
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2011-6-9 21:13:34
|
显示全部楼层
来自 上海
本帖最后由 JingheSu 于 2011-6-9 21:50 编辑
Python与Excel
目前处理数据离不开excel,所以pythoner必须学会用python操作excel表格。Python与excel交互方法也比较多,我一开始就接触的xlrd/xlwt包。直到现在也没有发现什么bug或者缺点,而且上次从ujs505那里知道Win32Com的方法在64bit系统下无效了,但是xlrd/xlwt方法依然好用,我欣喜万分。具体的用法我有一个帖子里面有说明:
http://forum.simwe.com/thread-978057-1-1.html
这里给出我一个从材料单轴拉伸数据TRA文件中处理得到拉伸过程中各种应力应变量并记录到excel中的例子,其实这个例子比较乱,但是实在没有时间给弄个明了的版本了。大家将就看看。。
重点看看其中
1 读取一行文本后如何分割出自己想要的数据(就是re包里面几个函数的应用)
- myfile='tensile-'+str(i+1)
- f=open(myfile+'.TRA','r')
- while state:
- s=f.readline()
- s=re.sub(';',' ',s)
- s=re.split(' +',s)
- # print s
- # print s0
- if (s=="EOF")|(s==s0):
- state=False
- else:
- force=float(s[1])
- eee=float(s[0])/100.0
- sss=force/area
- test_force.append(force)
- engineer_strain.append(eee)
- engineer_stress.append(sss)
- true_strain.append(log(1.0+eee))
- true_stress.append(sss*(1.0+eee))
- plastic_strain.append(log(1.0+eee)-sss*(1.0+eee)/E_modulus)
- f.close()
复制代码
2 python怎么读写excel那几行就行了。
- from xlwt import Workbook
- book=Workbook()
- sheet=book.add_sheet('test_NT',cell_overwrite_ok=True)
- sheet.write(0,nclown,'sample')
- sheet.write(0,nclown+2,name)
- sheet.write(1,nclown,"area")
- sheet.write(1,nclown+2,area)
- sheet.write(1,nclown+1,diameter)
- sheet.write(1,nclown+3,"E_modulus")
- sheet.write(1,nclown+4,E_modulus)
- sheet.write(2,nclown,"E_strain")
- sheet.write(2,nclown+1,"E_stress")
- sheet.write(2,nclown+2,"T_strain")
- sheet.write(2,nclown+3,"T_stress")
- sheet.write(2,nclown+4,"PL_strain")
复制代码
- import xlrd
- bk=xlrd.open_workbook('Static report.xls')
- sh=bk.sheet_by_name("Stat")
- E_modulus=sh.cell_value(i+2,7)
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
×
|