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

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