python操作excel

普通读写

写文件时,xlwt只能写65535行,如果有超过这个行数的,需要使用另外一个库openpyxl

# -*- coding: utf-8 -*-

import xlrd
import xlwt
from xlutils.copy import copy


def read_excel():
    """
    读取excel
    """
    # 打开
    workbook = xlrd.open_workbook('test_excel.xls')
    # 获取sheet,获取sheet的方式有很多,sheet_by_index只是一种
    sheet = workbook.sheet_by_index(0)
    # 遍历循环逐行逐列读取
    for row in range(0,sheet.nrows):
        for col in sheet.row_values(row):
            val=col.strip()
            print val

def create_excel():
    """
    新建excel
    """
    # 创建一个workbook 设置编码为utf-8
    workbook = xlwt.Workbook(encoding='utf-8')
    # 创建一个worksheet
    worksheet = workbook.add_sheet('Sheet1')
    # 写入worksheet,参数为(行、列、值)
    worksheet.write(0, 0, u"哈哈")
    worksheet.write(1, 1, u"biubiubiu")
    # 保存,目前不支持保存xlsx格式
    workbook.save('test_excel.xls')

def write_excel():
    """
    修改excel
    """
    # 先读excel
    workbook = xlrd.open_workbook('test_excel.xls')
    # 复制一份
    workbook_cp = copy(workbook)
    # 获取sheet
    sheet = workbook_cp.get_sheet(0)
    # 写入sheet
    sheet.write(2,0,"test_write")
    # 保存
    workbook_cp.save('test_excel.xls')

if __name__ == '__main__':
    create_excel()
    write_excel()
    read_excel()

openpyxl库

超过65535行的一般使用openpyxl库,用法有些略微差异, 需要注意的是,这里的第一行(列)就是文件中的第一行(列),和xlwt不一样,xlwt是第0行(列)对应文件的第一行(列)

import openpyxl
workbook = openpyxl.Workbook()
# 创建一个worksheet
worksheet = workbook.create_sheet(index=0)
# worksheet.cell(row, col, value)
worksheet.cell(1, 1, u"电梯编码")
worksheet.cell(1, 2, u"设备注册代码")
worksheet.cell(1, 3, u"所在区县")
workbook.save('test.xlsx')

一键安装FastDFS脚本

#!/bin/bash
# FastDFS安装脚本
# author cxinde

base_path=$(cd `dirname $0`; pwd)

log_info(){
    if [ $? -eq 0 ]
    then
        echo "----------------------${1}成功---------------------"
    else
        echo "----------------------${1}失败---------------------"
        exit
    fi
}

dirs=(download tracker storage mod_fastdfs file)
for dir_name in ${dirs[@]}; do
    if [ -e ${dir_name} ]
    then
        echo "${dir_name}文件夹存在"
    else
        mkdir ${dir_name}
        echo "${dir_name}文件夹不存在,创建成功"
    fi
done

echo "----------------------开始安装编译环境---------------------"

yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y

log_info "安装编译环境"

# 安装FDFS共用函数包
cd download
git clone https://github.com/happyfish100/libfastcommon.git --depth 1
cd libfastcommon/
./make.sh && ./make.sh install

log_info "FDFS共用函数包安装"

cd ..

# 安装fastdfs本体
git clone https://github.com/happyfish100/fastdfs.git --depth 1
cd fastdfs/
./make.sh && ./make.sh install

log_info "fastdfs本体安装"

cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
cp ./conf/http.conf /etc/fdfs/
cp ./conf/mime.types /etc/fdfs/


cd ..

# 安装fdfs的nginx中间件
git clone https://github.com/ireaderlab/fastdfs_nginx_module.git --depth 1
cd fastdfs_nginx_module
cp ./src/mod_fastdfs.conf /etc/fdfs

cd ..

# 安装nginx
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar -zxvf nginx-1.15.4.tar.gz
cd nginx-1.15.4/
./configure --add-module=../fastdfs_nginx_module/src/
make && make install

log_info "nginx安装"

read -p "本机是否需要安装Tracker:(Y/N)" has_tracker
read -p "请输入Tracker Server的IP:" tracker_server
read -p "请输入Tracker Server的端口:" tracker_port
read -p "请输入Nginx Server的端口:" nginx_port
if [ $has_tracker == 'Y' -o $has_tracker == 'y' ]
then
    cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
    sed -i "s!base_path=/home/yuqing/fastdfs!base_path=${base_path}/tracker!g" /etc/fdfs/tracker.conf
    service fdfs_trackerd start
fi
sed -i "s!base_path=/home/yuqing/fastdfs!base_path=${base_path}/storage!g" /etc/fdfs/storage.conf
sed -i "s!store_path0=/home/yuqing/fastdfs!store_path0=${base_path}/file!g" /etc/fdfs/storage.conf
sed -i "s/tracker_server=192.168.209.121:22122/tracker_server=${tracker_server}:${tracker_port}/g" /etc/fdfs/storage.conf
sed -i "s/http.server_port=8888/http.server_port=${nginx_port}/g" /etc/fdfs/storage.conf
sed -i "s!base_path=/tmp!base_path=${base_path}/mod_fastdfs!g" /etc/fdfs/mod_fastdfs.conf
sed -i "s/tracker_server=tracker:22122/tracker_server=${tracker_server}:${tracker_port}/g" /etc/fdfs/mod_fastdfs.conf
sed -i "s/url_have_group_name = false/url_have_group_name = true/g" /etc/fdfs/mod_fastdfs.conf

sed -i '38a \\tlocation ~/group[0-9]/ {\n\t    ngx_fastdfs_module;\n\t}' /usr/local/nginx/conf/nginx.conf
sed -i "s/listen       80;/listen       ${nginx_port};/g" /usr/local/nginx/conf/nginx.conf

service fdfs_storaged start
/usr/local/nginx/sbin/nginx

echo "FastDFS安装完成"