python 读取字典类型的文本和读取excel文件


一,读取文本:

比如D盘有个文本文件TestConf.ini,它的内容是一个字典:{"tester":"sterson","projectName":"baidu"}

def read_dict_conf(conf_path):
     f = open(conf_path, "r")
     content_dict = eval(f.read())
     f.close()
     return content_dict
 
 
 # 调用
 test_config = read_dict_conf('d:TestConf.ini')
 
 # 读取projectName ,读出来的值就是baidu
 proName = test_config["projectName"]
python 读取字典类型的文本和读取excel文件


二,读取excel文件

通过python读取excel里的内容,用到包 xlrd。

import xlrd
# filePath:字符串,Excel文件名称
# workTable:字符串,Excel文件中(sheet)表格名称
def read_excel_data(file_path, work_table):
try:
# 这里要用decode,否则中文乱码,读不到文件
data_name = xlrd.open_workbook(file_path.decode('utf8'))
table = data_name.sheet_by_name(work_table.decode(
'utf8'))
except:
print '%s文件打开失败' % (file_path)
return table

test_data = read_excel_data('d:t.xls', 'Sheet1')
 irows = test_data.nrows
 for td in xrange(1,irows):
     test_name = test_data.cell(td, 0).value
     test_sex = test_data.cell(td, 1).value
     test_class = test_data.cell(td, 2).value
     print '%s%s%s%s%s' % (test_name, ', ', test_sex, ', ', test_class)
展开阅读全文

页面更新:2024-04-24

标签:字典   文件   乱码   字符串   中文   文件名称   表格   文本   名称   类型   内容   科技   文本和

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top