python获取excel整行数据如何保存到新的工作簿中_Python复制已有excel中的某几行到新的excel工作表...

匿名用户

1级

2014-05-28 回答

python操作excel的类库可以用这个:

http://www.python-excel.org/

Github上有范例:

https://github.com/python-excel/tutorial/blob/master/tests/test_examples.py

我摘抄一段吧,

import os

from cStringIO import StringIO

from glob import glob

from os import path, environ

from os.path import abspath

from re import compile

from shutil import copy

from subprocess import call, STDOUT

from tempfile import TemporaryFile

from testfixtures import TempDirectory, compare

from xlrd import Book, biff_dump

initial = os.getcwd()

base = abspath(path.join(path.dirname(abspath(__file__)), os.pardir))

runner = abspath(path.join(base, 'bin', 'py'))

examples = path.join(base, 'students')

expected = path.join(base, 'tests', 'expected')

sub_res = [

(compile('0+x[0-9A-Fa-f]+'), '...'),

(compile('".+'+os.sep.replace('\\','\\\\')+'(.+.py)"'), '"\\1"'),

]

def get_biff_records(data):

outfile = StringIO()

bk = Book()

bk.biff2_8_load(file_contents=data, logfile=outfile, )

biff_dump(bk.mem, bk.base, bk.stream_len, 0, outfile, unnumbered=True)

return outfile.getvalue()

def check_example(package, filename):

example_dir = path.join(examples, package)

expected_dir = path.join(expected, package)

expected_base = path.join(expected_dir, path.splitext(filename)[0])

try:

with TempDirectory() as actual:

# copy files to the directory

copy(path.join(example_dir, filename), actual.path)

for pattern in ('*.xls', '*.bmp'):

for fixture in glob(path.join(example_dir, pattern)):

copy(fixture, actual.path)

os.chdir(actual.path)

output = TemporaryFile('w+')

# run the example

before_listing = set(os.listdir(actual.path))

call([runner, filename], stdout=output, stderr=STDOUT)

after_listing = set(os.listdir(actual.path))

# check the console output

output.seek(0)

actual_output = output.read().strip().replace('\r', '')

for re, rp in sub_res:

actual_output = re.sub(rp, actual_output)

expected_path = expected_base+'.txt'

if not path.exists(expected_path):

expected_output = ''

else:

expected_output = open(expected_path).read().strip().replace('\r', '')

compare(expected_output, actual_output)

# check the files created

created = after_listing.difference(before_listing)

expected_names = set()

if os.path.exists(expected_base):

expected_names = set(os.listdir(expected_base))

for name in created:

with open(path.join(actual.path, name), 'rb') as af:

actual_data = af.read()

if name in expected_names:

expected_path = path.join(expected_base, name)

expected_data = open(expected_path, 'rb').read()

expected_names.remove(name)

if actual_data != expected_data:

if environ.get('REPLACE_EXAMPLES'):

with open(expected_path, 'wb') as new_expected:

new_expected.write(actual_data)

compare(

get_biff_records(expected_data),

get_biff_records(actual_data),

)

else:

raise AssertionError("unexpected output: %s" % name)

for name in expected_names:

if name != '.svn':

print created

raise AssertionError("expected output missing: %s" % name)

finally:

os.chdir(initial)

def test_examples():

for package in ('xlrd', 'xlwt', 'xlutils'):

for py in glob(path.join(examples, package, '*.py')):

yield check_example, package, path.split(py)[1]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值