Python常用操作

console 输入txt


#利用输出重定向输出两次 

#同样输出程序path和文件名


import os

importsys

temp=sys.stdout# 记录当前输出指向,默认是consle

withopen("outputlog.txt","a+") as f:    

     sys.stdout=f # 输出指向txt文        print("filepath:",__file__,"\nfilename:",os.path.basename(__file__))    

    print("some other information")    

    print("some other")    

    print("information")    

    sys.stdout=temp# 输出重定向回consle

    print(f.readlines())# 将记录在文件中的结果输出到屏幕

pandas 索引列互相转换


# 列 ——> 索引

df.set_index('date')

df.set_index('date', inplace=True)# column 改为 index

#索引 ——> 列

df['index'] = df.index

df.reset_index(level=0, inplace=True)

df.reset_index(level=['tick', 'obs'])

df['si_name'] = df.index.get_level_values('si_name') # where si_name is the name of the subindex.

df.reset_index()# (all)index 改为 column

#df.reset_index(level=0, inplace=True) # (the first)index 改为 column

中文路径乱码问题:


data_dir = 'D:\百分点工作资料\PHM技术资料\_871PHM_贝晓强\871PHM\871PHM\data_dictionary\data1.csv'

strPath = data_dir.decode('utf-8')

  • initialize many lists:

# wrong: 

x = y = z = []

# right:

alist, blist, clist, dlist, elist = ([] for i in range(5))

do not show the figure


import matplotlib.pyplot as plt

# Turn interactive plotting off

plt.ioff()

  • 注释 ctrl /

  • 整段注释不支持?

continue语句跳出本次循环,而break跳出整个循环

安装某些包:

sudo apt-get install python3-tk

import tkinter

-定义的函数之间要隔开两行

range(start, stop, step)

-替换:Ctrl+R.

  • 删除当前行 >CTRY Y

ALT F7: 查找哪些地方使用了选中的方法。

ALT UP: 移到上一个方法.

ALT DOWN: 移到下一个方法.

CTRL SHIFT UP: 将当前行上移一行.

CTRL SHIFT UP: 将当前行下移一行.

Alt+Enter 自动添加包

see the modules in the terminal (Python3):


sudo apt-get install python-pip3

pip3 freeze 

pip3 freeze | grep feed

will list all packages about "feed'

update python 3.4 to python 3.6 in ubuntu 14.04:

< http://devopspy.com/python/install-python-3-6-ubuntu-lts/>


#optional 

sudo apt-get update

sudo apt-get install build-essential libpq-dev libssl-dev openssl libffi-dev zlib1g-dev

sudo apt-get install python3-pip python3-dev

# adding from PPA

sudo add-apt-repository ppa:jonathonf/python-3.6

sudo apt-get update

sudo apt-get install python3.6

error in import matplotlib:


# in terminal

sudo apt-get install python3-tk 


# in IDE:

import tkinter 

你可能感兴趣的:(Python常用操作)