您的位置:首页 > 编程语言 > Python开发

python学习之一 文件读取

2017-09-11 15:29 155 查看

python学习之一 文件读取

[b]文件逐行读取[/b]

import os
with open("***", "r") as f:
for line in f.readlines():
list = line.split(",")
print(list)


[b]Excel表格操作[/b]

# -*- 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python excel