python


原文链接: python

Book

python3-cookbook 3.0.0 文档
利用 Python 进行数据分析 · 第 2 版

雨敲窗Python
python开发大全、系列文章、精品教程 - 全栈工程师开发手册(原创) - CSDN博客
Python数据科学速查表
看云 Python开发入门 常见问题
牛顿迭代法 · 用Python学微积分
1.4 Matplotlib:绘图

#启动http server
python -m SimpleHTTPServer
python3 -m http.server

miniconda 集成vscode

  1. shift + cmd + P
  2. Search Select Interpreter

    main

    ```py
    #!/usr/bin/env python

    -- coding: utf-8 --

    python2 兼容python3

    from future import print_function

import os
if name == 'main':

sys.exit(main())
```py
def copyFile(source, target):
    open(target, "wb").write(open(source, "rb").read())

解决: Fatal Python error: init_sys_streams: can't initialize sys standard streams

LookupError: unknown encoding: 65001

方法1. chcp 936 chcp 1252

built-in

ord('a') -> 97 "ordinal"序数: 返回对应的 ASCII 数值,或者 Unicode 数值

class MySuper(object):
    def __init__(self,a):
        self.a = a

class MySub(MySuper):
    def __init__(self,a,b):
        self.b = b
        super().__init__(a)

my_sub = MySub(42,'chickenman')
print(my_sub.a)
print(my_sub.b)

python文件分段的两种方式

方式一:# In[]:

方式二:#%%

仅读取文件

only_files = [f for f in listdir(path) if isfile(join(path, f))]
zip

full_streets = [str(x) + ' '+ y + ' '+ z for x,y,z in zip(numbers, streets, street_suffs)]
street_names = ['abbey','baker','canal','donner','elm']
street_types = ['rd','st','ln','pass','ave']
rand_zips = [random.randint(65000,65999) for i in range(5)]
numbers = [random.randint(1,9999) for i in range(n)]
streets = [random.choice(street_names) for i in range(n)]
street_suffs = [random.choice(street_types) for i in range(n)]
zips = [random.choice(rand_zips) for i in range(n)]
full_streets = [str(x) + ' '+ y + ' '+ z for x,y,z in zip(numbers, streets, street_suffs)]
reference_data = [list(x) for x in zip(full_streets, zips)]

json 文件读取

import json
with open('jsonfile.json') as js:
ss = json.load(js)
print(ss)

js = json.load(open('jsonfile.json'))
print(js)

文件写入
import json
number = [1, 2, 3, 4, 5, 6]
filename = 'jsonfile.json'
with open(filename, 'w')as js:

json.dump(number,js)

csv 文件读取

filename = 'mesg.csv'            import csv
with open(filename)as fn:          fn = csv.reader(open(filename,'r'))
name = fn.read()               for i in fn:
print(name)                 print(i)

import csv
csvfile = open(filename,'r')
reader = csv.reader(csvfile)
for row in reader:

print(row)
`