1. 输出CSV文件

用python输出csv文件不难,可是MS office excel和WPS 对输出的CSV文件打开规则不一样。

WPS可以支持CSV以'\t'为分隔符,打开文件直接写内容

MS office excel必须用','为分隔符,打开文件在写内容之前,要先写入文件头:u"\ufeff",否则输出的中文会出现乱码。

import codecs

csvstr = 'test'
fh = codecs.open("myfile.csv","w","utf-8")
fh.write(u"\ufeff")
fh.write(csvstr)
fh.close()

2. 使用pyInstaller打包python程序

pyInstaller包目前可以直接使用pip install pyinstaller来安装,安装完成之后,在命令行输入pyinstaller命令,会出现参数提示信息。

D:\Source\Python>pyinstaller
usage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME]
[--add-data <SRC;DEST or SRC:DEST>]
[--add-binary <SRC;DEST or SRC:DEST>] [-p DIR]
[--hidden-import MODULENAME]
[--additional-hooks-dir HOOKSPATH]
[--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES]
[--key KEY] [-d [{all,imports,bootloader,noarchive}]] [-s]
[--noupx] [-c] [-w]
[-i <FILE.ico or FILE.exe,ID or FILE.icns>]
[--version-file FILE] [-m <FILE or XML>] [-r RESOURCE]
[--uac-admin] [--uac-uiaccess] [--win-private-assemblies]
[--win-no-prefer-redirects]
[--osx-bundle-identifier BUNDLE_IDENTIFIER]
[--runtime-tmpdir PATH] [--bootloader-ignore-signals]
[--distpath DIR] [--workpath WORKPATH] [-y]
[--upx-dir UPX_DIR] [-a] [--clean] [--log-level LEVEL]
scriptname [scriptname ...]
pyinstaller: error: the following arguments are required: scriptname

pyinstaller命令后scriptname参数是必须的,这是要生成exe的脚本文件,其都是可选参数,如果直接用pyinstaller scriptname来打包,会生成很多dll文件,如果想打包成一个可执行程序,要加参数-F,pyinstaller -F scriptname,其他参数下如图所示。

要打包的脚本文件要注意import的包,尽量只import需要的包和类,否则生成的文件可能会很大。

3. 获取当前文件路径,上级目录路径

import os,sys

使用sys.path[0]、sys.argv[0]、os.getcwd()、os.path.abspath(__file__)、os.path.realpath(__file__)

sys.path是Python会去寻找模块的搜索路径列表,sys.path[0]和sys.argv[0]是一回事因为Python会自动把sys.argv[0]加入sys.path。

如果你在C:\test目录下执行python getpath\getpath.py,那么os.getcwd()会输出“C:\test”,sys.path[0]会输出“C:\test\getpath”。

获取当前的路径:

__file__是当前执行的文件

# 获取当前文件__file__的路径
print(os.path.realpath(__file__))
# 获取当前文件__file__的所在目录
print(os.path.dirname(os.path.realpath(__file__)))
print(os.path.abspath(os.path.dirname(__file__)))
print(os.path.split(os.path.realpath(__file__))[0])
# 获取上级目录
print(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
print(os.path.abspath(os.path.join(os.path.dirname('APPtest\\RSA.py'),'.\..')))
# 获取上上级目录
print(os.path.abspath(os.path.join(os.path.dirname('APPtest\\RSA.py'),'../..')))

python杂记一的更多相关文章

  1. python杂记-4(迭代器&生成器)

    #!/usr/bin/env python# -*- coding: utf-8 -*-#1.迭代器&生成器#生成器#正确的方法是使用for循环,因为generator也是可迭代对象:g = ...

  2. python杂记-3(购买商品)

    #!/usr/bin/env python# -*- coding: utf-8 -*-#如下是一个购物程序:#先输入工资,显示商品列表,购买,quit退出,最后格式化输出所买的商品.count = ...

  3. python杂记-1(os模块)

    os模块说明:python os模块包含普遍的操作系统功能 os.access(path, mode) # 检验权限模式 os.chdir(path) # 改变当前工作目录os.chflags(pat ...

  4. python杂记-6(time&datetime模块)

    #!/usr/bin/env python# -*- coding: utf-8 -*-import timeprint(time.clock())##返回处理器时间,3.3开始已废弃 , 改成了ti ...

  5. python杂记-5(装饰器)

    1.被装饰的函数有参数(一个参数): def w1(func): def inner(arg): # 验证1 # 验证2 # 验证3 return func(arg) return inner @w1 ...

  6. python 杂记

    class TestA(object): def __init__(self): print("A is initing"); def foo(self): print(" ...

  7. Python杂记

    一.函数 1.numpy 模块中的nonzero函数 nonzero返回的数非零元素的下标. 如果输入是单维度的时候它的返回值只有一个:如果输入是多个维度的话,那么它的返回值也是多个维度的.并且的它的 ...

  8. python杂记二

    1. 写文件可以直接使用print函数 file_name = open("file_name.txt","w") print("file conta ...

  9. Python——杂记

    python 最近出错总结: 1.而for..in ..中不要用else if  x in y:     print  else:     print2.def fibs(num): ...     ...

随机推荐

  1. C#手动改变自制窗体的大小

    Form1.cs using System;using System.Collections.Generic;using System.ComponentModel;using System.Data ...

  2. php中wampserver多站点配置

    1.修改默认端口 : 2.添加多站点: 3.在文件的结尾添加一个站点配置: <VirtualHost *:8080> ServerAdmin webmaster@duoduo.com Do ...

  3. 剑指Offer 12. 数值的整数次方 (其他)

    题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 题目地址 https://www.nowcoder.com/practice/ ...

  4. 安装Python 3.6 在Ubuntu 16.04 LTS 版本

    Collecting tensorflow Could not find a version that satisfies the requirement tensorflow (from versi ...

  5. 【Java】字符串工具类

    import android.annotation.SuppressLint; import java.io.UnsupportedEncodingException; import java.uti ...

  6. Python全栈之路----函数----匿名函数

    用lambda声明匿名函数,对lambda定义名字,才能被调用.下面的calc和func功能一致. def calc(x,y): return x*y func = lambda x,y: x*y # ...

  7. [Java] 例外處裡 try/catch & throws

    public class CheckException { public static void main(String[] args) { File file = new File("xx ...

  8. Linux scp命令详解

    Linux scp命令 Linux scp命令用于Linux之间复制文件和目录. scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令. 语法: ...

  9. php执行系统命令的四个函数shell_exec, exec, passthru, system分别的使用场景

    shell_exec() 通过 shell 环境执行命令,并且将完整的输出以字符串的方式返回.也就是说, PHP先运行一个shell环境, 然后让shell进程运行你的命令, 并且把所有输出已字符串形 ...

  10. FPGA 中三角函数的实现

    FPGA 中三角函数的实现