python学习之一 文件读取

python学习之一 文件读取

文件逐行读取
import os
with open("***", "r") as f:
    for line in f.readlines():
        list = line.split(",")
        print(list)

Excel表格操作
# -*- coding: utf-8 -*-

# Excel表格操作

import os

import xlrd
import xlwt
import re
import time
from xlrd import open_workbook
from xlutils.copy import copy


# 文件路径

fileUrl='E:/qingdan/2017-09-08.xls'


# 数据读取
data = xlrd.open_workbook(fileUrl) # 打开xls文件
table = data.sheets()[0] # 打开第一张表
nrows = table.nrows # 获取表的行数
# sheet_name = data.sheet_names()[0]  # 获得指定索引的sheet名字
#


# 修改表格数据
rb = open_workbook(fileUrl, formatting_info=True)
rs = rb.sheet_by_index(0)
wb = copy(rb)
ws = wb.get_sheet(0)


# 读取并修改数据
stimes=0
for n in range(nrows):
    prices=table.row_values(n)[17]
    # print prices
    if n==0:
        stimes=time.time()
        print stimes
    elif len(prices.split(u"减"))==2:
        pr=prices.split(u"减")[1].split(u"元")[0]
        # print pr
        ws.write(n, 17,pr)
    elif len(prices.split(u"条"))==2:
        pr=prices.split(u"元")[0]
        # print pr
        # print prices.split(u"元")[0]
        ws.write(n, 17, pr)

wb.save(fileUrl)
print time.time()-stimes

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章