1、串口連接
這種方式在我樹莓派的第一篇博客有講,這里我簡單介紹。
連接樹莓派,樹莓派GPIO串口的GND,txd,rxd分別與轉(zhuǎn)接口的GND,rxd,txd相連,用的什么轉(zhuǎn)接模塊我不管,反正最后得變成一個USB的接口接在電腦上,并在電腦設(shè)備管理器上的端口選項可以發(fā)現(xiàn)它,用Serial登錄。
串口登錄沒有多色高亮,所以用wifi登錄,配置文件路徑在路徑為/etc/wpa_supplicant/wpa_supplicant.conf。
可以用PUTTY登錄,我用的是新版的secureCRT。
串口線有4根,黑色表示gnd。紅色vcc,另外的綠色和白色表示rxd和txd(順序未知)。根據(jù)測試表明為rxd接到板子上的txd.另個綠色一則相反為txd.使用Serial登錄。選擇對應(yīng)的COMx,選擇波特率為115200。
系統(tǒng)默認的用戶名為pi,密碼為raspberry
Serial登錄之后配置ssh。這里我事先配置過了,查看ip的命令為ifconfig.得到共享熱點為樹莓派分配的ip為192.168.43.67。。使用putty第一次ssh登錄樹莓派,電腦會彈窗警告。點擊是即可。
使用nano編輯器對配置文件修改。
啟用或者禁用使用SSH遠程終端登錄你的樹莓派。
SSH允許你在另外一臺計算機上使用命令行終端遠程登錄你的樹莓派。禁用將會導致SSH服務(wù)不會在開機的時候啟動以釋放程序的資源。注意SSH默認是啟用的。如果你的樹莓派直接連接到公共網(wǎng)絡(luò),你應(yīng)該禁用SSH,除非你確認已經(jīng)為樹莓派上每一個用戶設(shè)置了密碼。
/boot放置Linux內(nèi)核以及其他用來啟動樹莓派的軟件包
/bin放置與Raspbian有關(guān)(包括運行圖形界面所需的)的二進制可執(zhí)行文件
/dev這是虛擬文件夾之一,用來訪問所有連接設(shè)備,包括存儲卡
/etc系統(tǒng)管理和配置文件
/homeLinux上的我的文檔,包含用戶名命名的文件夾
/lib各種應(yīng)用需要的代碼庫
/lost+found一般情況下是空的,當系統(tǒng)非法關(guān)機后,這里就存放了一些文件
/media放置可移動存儲驅(qū)動器,比如USB和CD
/mnt用來手動掛在外部硬件驅(qū)動器或存儲設(shè)備
/opt可選軟件文件夾,非系統(tǒng)部分的軟件將會放置在這里
/proc另一個虛擬文件夾,包含運行進程(或程序)的信息
/selinux由美國國家安全局開發(fā)的安全工具,用于增強Linux安全性
/sbin放置超級用戶使用的系統(tǒng)管理命令
/sys放置操作系統(tǒng)文件
/tmp放置臨時文件
/usr放置用戶使用的程序
/var虛擬文件,用于程序保存數(shù)據(jù)
***********************************
使用cd /boot實現(xiàn)文件路徑跳轉(zhuǎn)。在使用ls列出改路徑下的文件夾和文件。用綠色的著色表示文件,藍色的著色表示文件夾。
cd overlays,直接跳轉(zhuǎn)到該文件夾下。cd /表示跳轉(zhuǎn)到根目錄下。
跳到自己的 home 目錄 :
cd ~
在根目錄下用ls列出所有文件和文件夾。
bin dev home ?lost+found mnt proc run srv tmp varcd ?boot ect lib media opt root sbin sys usr
cd ..表示向高層回跳一級。
在根目錄下使用
cd /home 之后使用cd pi得到的結(jié)果和cd ~的結(jié)果是完全相同的。
******************************
Linux cd命令用于切換當前工作目錄至 dirName(目錄參數(shù))。
其中 dirName 表示法可為絕對路徑或相對路徑。若目錄名稱省略,則變換至使用者的 home 目錄 (也就是剛 login 時所在的目錄)。
另外,"~" 也表示為 home 目錄 的意思,"." 則是表示目前所在的目錄,".." 則表示目前目錄位置的上一層目錄。
Linux ls命令用于顯示指定工作目錄下之內(nèi)容(列出目前工作目錄所含之文件及子目錄)。
ls -R列出文件夾下的路徑及文件。
語法
**********************
Linux mkdir命令用于建立名稱為 dirName 之子目錄。
mkdir sangshu,在pi下面建立一個sangshu的文件夾。該命令用于創(chuàng)建文件夾目錄。
使用nano編輯器,nano anbao.txt,輸入內(nèi)容退出保存,提示保存,按y.再回車確定即可。(原先沒有文件將創(chuàng)建輸入的文件名的文件)
將工作目錄下,名為 AAA 的子目錄刪除 :
rmdir AAA;;;rmdir shu.vim(這是一個文件夾目錄);;不能刪除文件,只能是文件夾目錄。
rm可以用來刪除文件。rm anbao.txty;;;;
使用who命令查看誰登陸過該系統(tǒng)。
***************************************************
find home返回所有home的子路徑文件。這樣只能查詢下一級的文件。下下級將不能查找到。
Linux find命令用來在指定目錄下查找文件。任何位于參數(shù)之前的字符串都將被視為欲查找的目錄名。如果使用該命令時,不設(shè)置任何參數(shù),則find命令將在當前目錄下查找子目錄與文件。并且將查找到的子目錄和文件全部進行顯示。
find . -name "*.c"返回當前目錄下的.c文件。find . .-name "*.c"上一級路徑下的.c文件。
路徑為/etc/wpa_supplicant/wpa_supplicant.conf
直接使用nano不被允許打開,使用sudo nano wpa_supplicant.conf即可。
修改密碼之后如果使用ssh登陸仍然不會斷開,使用wifi重啟命令將會使其斷開。
在配置文件路徑下使用重啟wifi命令失敗,sudo service network restart。。。
在pi路徑和根目錄下仍然失敗。那就使用sudo reboot吧
進入python之后使用quit()即可退出python重新進入home界面。
輸入python2和python3進入各自程序。
******************************************************
sudo vi wpa_supplicant.conf進入vi命令模式之后
打開vim使用命令為vi。。使用冒號:加關(guān)鍵字寫入vi命令模式。
:w //保存文件
:q //退出編輯器,如果文件已修改請使用下面的命令
:q! ?//退出編輯器,且不保存
:wq ?//退出編輯器,且保存文件
vi有3個模式:插入模式、命令模式、低行模式。
*****************************
a????? //在當前光標位置的右邊添加文本
i?????? //在當前光標位置的左邊添加文本
A???? //在當前行的末尾位置添加文本
I????? //在當前行的開始處添加文本(非空字符的行首)
O???? //在當前行的上面新建一行
o???? //在當前行的下面新建一行
R??? //替換(覆蓋)當前光標位置及后面的若干文本
J??? //合并光標所在行及下一行為一行(依然在命令模式)
這幾個鍵將改變命令模式為插入模式。esc將是插入模式退為命令模式,再使用:u
撤銷一步。
插入文本或行(vi命令模式下使用,執(zhí)行下面命令后將進入插入模式,按ESC鍵可退出插入模式)
命令模式下的上下左右鍵才有意義。插入模式下沒有方向鍵無意義。
dd????? //刪除當前行
***************************
NAS(Network Attached Storage:網(wǎng)絡(luò)附屬存儲)按字面簡單說就是連接在網(wǎng)絡(luò)上,具備資料存儲功能的裝置,因此也稱為"網(wǎng)絡(luò)存儲器"。它是一種專用數(shù)據(jù)存儲服務(wù)器。
直接在根目錄下用mkdir share無法成功,用sudo mkdir share才行。如果目錄已經(jīng)存在的話將無法再創(chuàng)建一個同名字的目錄。報錯files exist
***********************************
Linux/Unix 的文件調(diào)用權(quán)限分為三級 : 文件擁有者、群組、其他。利用 chmod 可以藉以控制文件如何被他人所調(diào)用。
使用權(quán)限?: 所有使用者
此外chmod也可以用數(shù)字來表示權(quán)限如 :
chmod 777 file
語法為:
chmod abc file
其中a,b,c各為一個數(shù)字,分別表示User、Group、及Other的權(quán)限。
r=4,w=2,x=1
若要rwx屬性則4+2+1=7;
若要rw-屬性則4+2=6;
若要r-x屬性則4+1=5。
********************************
設(shè)置一個公共目錄
cd /;sudo mkdir share;sudo chmod 777 share
sudo vim /etc/samba/smb.conf
在最后添加:
[share]
comment = pi share
path = /share
browseable = yes
read only = no
writable = yes
public = yes
6.重啟samba服務(wù)使之生效
sudo service smbd restart,重啟失敗。這命令不能實現(xiàn)。
sudo shutdown 關(guān)機,sudo shutdown -c關(guān)機取消。。。
****************************************
whereis python查詢命令的方式。
Ctrl+D, ?或?Quit[]退出wolfram的文本界面,https://reference.wolfram.com/language/tutorial/UsingATextBasedInterface.html,文本界面的使用命令。
***********************************************
環(huán)境變量,用分號隔開。最后一位沒有分號,如需添加新的環(huán)境變量,繼續(xù)加上分號和路徑。。
D:\Python\Python36-32\Scripts\;
D:\Python\Python36-32\;
%SystemRoot%\system32;
%SystemRoot%;
%SystemRoot%\System32\Wbem;
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;
C:\Program Files (x86)\Wolfram Research\WolframScript\;
D:\MATLAB\R2018a\runtime\win64;
D:\MATLAB\R2018a\bin
在dos下的快捷啟動,python,MATLAB,wolframscript
**************************************************
添加多個wifi連接,/etc/wpa_supplicant/wpa_supplicant.conf修改再下面添加一個新的。
sudo poweroff關(guān)機
sudo reboot重啟
sudo raspi-config配置界面。
whoami輸出用戶,cd,cat,mkdir,ls,pwd..
calendar返回歷史事件。。
cd和cd ~的作用相同。。
pi賬戶必須用sudo才能執(zhí)行某些命令。。獲取root賬戶即可。。
BAD PASSWORD: it is based on a dictionary word”這是因為出現(xiàn)了字典里的字符串。
如果你采用英文與數(shù)字組合使用,且長度在6位以上,就不會報錯。
sudo apt-get dist-upgrade更新軟件。。
sudo apt-get install ****
E:unable to fetch some aechives 可能是網(wǎng)絡(luò)原因。。
python -V默認python2
Python 2.7.13
pi@raspberrypi:~ $ python3 -V大寫。。
Python 3.5.3
$ nano -V版本返回
sudo apt-get install python nano軟件之間用空格隔開。。
python是解釋型而不是編譯型語言。。
ctrl+d或quit()或者exit()退出python3,,help()進入help>int,退出函數(shù)查詢按q,退出help,。ctrl+d或者quit()。
$ cat py3prog/sample.py
print("here is the line dog.")
pi@raspberrypi:~ $ python3 py3prog/sample.py
here is the line dog.
g,x,o,nano編輯器幫助,退出,寫入。
\u03c0是pi..unicode
*****************************************
import keyword關(guān)鍵字打出來。。
>>> print("keyword.kwlist")
keyword.kwlist
>>> print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
print("12","56","89",sep='*')這種用法。。print("1256","89",end='!\n'),,sep和end的用法。。
type("456")返回數(shù)據(jù)類型。。
a=input("請輸入:")
請輸入:56
>>> type(a)
<class 'str'>類型str..
b=int(a)
>>> type(a)
<class 'str'>
>>> type(b)
<class 'int'>
*****************
0b01001110二進制表示。。
c=a&b
>>> bin(c)
'0b1000101'
>>> oct(c)
'0o105'
>>> hex(c)
'0x45'
chmod +x script0401.py之后變?yōu)榫G色。。修改權(quán)限
chmod u+x script0401.py
**************************************
腳本語言的第一行,目的就是指出,你想要你的這個文件中的代碼用什么可執(zhí)行程序去運行它
#!/usr/bin/python是告訴操作系統(tǒng)執(zhí)行這個腳本的時候,調(diào)用/usr/bin下的python解釋器。
#!/usr/bin/env python這種用法是為了防止操作系統(tǒng)用戶沒有將python裝在默認的/usr/bin路徑里。當系統(tǒng)看到這一行的時候,首先會到env設(shè)置里查找python的安裝路徑,再調(diào)用對應(yīng)路徑下的解釋器程序完成操作。這種寫法會去環(huán)境設(shè)置尋找python目錄
print("12233{}".format(12))
1223312
>>> print("12233{}".format("56")),,format的用法。。
1223356
a=5.2*9;
>>> print("fggg{}".format(a))
fggg46.800000000000004
>>> print("fggg{0:.2f}".format(a))
fggg46.80
分數(shù)對象,,from fractions in Fraction
b=Fraction(1,5)
>>> b
Fraction(1, 5)
>>> print(b)
1/5
*************************
b=Fraction(5.5)
>>> print(b)
11/2
print(complex(1,2))
(1+2j)
math.factorial(5)階乘。。
ceil,copysign,,fabs,factorial,floor,fmod,frexp,fsum,isfinite,isinf,isnan,ldexp,modf,trunc
from math import *,星號必須加上。。或者import math 使用math.ceil()
math+點+函數(shù)這種。。
math.fsum([1,2,3])
> x=-2
>>> y=6
z=math.copysign(y,x)
>>> z
-6.0
*********************************
math.fmod(5,3)
2.0
**************************
第一種,,pip3 install numpy
第二種采用源碼安裝:步驟如下:
1.下載源碼包:
wget http://jaist.dl.sourceforge.net/project/numpy/NumPy/1.9.0/numpy-1.9.0.zip
2.解壓
unzip numpy-1.9.0.zip
3.進入解壓目錄
cd numpy-1.9.0
4.運行解壓目錄里的setup.py 文件(root環(huán)境下)
python setup.py install
用nano編寫python程序時注意if(x>0):空出4個空格。。tab鍵不對。。
x="35"判斷能否轉(zhuǎn)換為數(shù)字。。
>>> x.isdigit()
x="jk"
>>> x.isdigit()
False
for letter in 'Python':print(letter)
for letter in [1,2,3,6,8]:注意格式。。
...? ? print(letter)
**********************
for i in range(1,5):
...? ? print(i)--0***4
for i in range(5):
...? ? print(i)--1***4
******************
for i in range(1,5,2):2為步長。。
...? ? print(i)
在idle中,用死循環(huán),while True:print(1),一直刷屏,怎么退出,ctrl+c,退出循環(huán)或者ctrl+z,直接退出python idle。。
tuplel=()
>>> print(tuplel)
(),,,, type(tuplel)
<class 'tuple'>
tuple([*,*,*,*])元組是不可改變的。。不能添加和刪除值。。可以訪問,
tuole6=(1,2,3,4)
>>> print(tuole6[2])
print(tuole6[1:3])
(2, 3)一側(cè)省略表示剩下全部,,print(tuole6[1:])
> print(tuole6[1::2])加上步長。。
(2, 4)
*************************************
len(tuole6)獲取元組的長度,,if 3 in tuole6:判斷值是否在元組中,,
4
min,max
(1,2,2)+(5,8,9)元組加法。。
(1, 2, 2, 5, 8, 9)
**********************
列表是可變的,,
list1=[],,
tuple11=1,2,3,4,5
list(tuple11)或者tuple(tuple11)創(chuàng)建列表和元組。。
元組和列表都可以使用負數(shù)作為索引。。[*:*:*]切片來提取子列表
list1[1]=10,修改列表第二項的值。。
list1[1:3]=1,6
opl=4,0,2,type(opl),tuple類型。。用元組修改列表,,
del list1[1]
>>> list1
[1, 0, 9],,,,del list1[1:3]
list6.pop(5),彈出列表值,原列表該值刪掉。
list6.pop()默認最后一個數(shù)。。
list1.append(5)
>>> list1
[1, 5]
list1.insert(0,"a")
>>> list1
['a', 1, 5]
append和pop堆棧操作,先入后出,list1=[10,20,30],list1.pop(),list1.pop(),list1.pop(),彈3次分別出來30,20,10
[1,2,3]+[4,5,6]
list5.append([5,6])
>>> list5
[1, 2, 3, [5, 6]]
list5.extend([7,0]),合并列表格式,去掉嵌套列表。。
>>> list5
[1, 2, 3, [5, 6], 7, 0]
*******************
list5=[1,2,3,4,1,2,3,5]
>>> list5.count(1)
list5.sort(),,,,sort()和sorted的用法,list5.sort(),,,sorted(list5),reversed(list5)
>>> list5
[1, 1, 2, 2, 3, 3, 4, 5],,,, list5.index(5)
7,,,給出索引號碼。。
list13=[[1,2,3],[4,5,6],[7,8,9]]
reversed(list5)
<list_reverseiterator object at 0xb6a0e710>
>>> sorted(list5)
[1, 1, 2, 2, 3, 3, 4, 5],,,,注意reversed的返回值不是列表。。用遍歷訪問或者換為list5.reverse()
print(reversed(list5))
<list_reverseiterator object at 0xb6a0e770>
[1, 1, 2, 2, 3, 3, 4, 5]
>>> list5.reverse()
>>> list5
[5, 4, 3, 3, 2, 2, 1, 1],,,list5.reverse()可以直接返回倒序列表。。
******************************
創(chuàng)建列表,列表解析法。。
list6=[x*2 for x in list5]
>>> list6
[10, 8, 6, 6, 4, 4, 2, 2]
*********************
> list5=["a","g","i","l"]
>>> list6=[x.upper() for x in list5]
>>> list6
['A', 'G', 'I', 'L']
*********************
list7=[x.lower() for x in list6]
>>> list7
['a', 'g', 'i', 'l']
************
range5=range(5)
>>> print(range5)
range(0, 5),,range像列表一樣是一種類型。。只能單個引用或遍歷顯示。。
python2中range()屬于list,python3成為一種新的類型。。
student={}
>>> type(student)
<class 'dict'>
定義字典是無序排列的。。
student["8"]=56
>>> student
{'h': '8', 1: 2, 'l': 7, '8': 56}。。。
student[1]
2
>>> student["8"]
56
>>> student["h"]值通過鍵得獲取。。字典是不能使用例如index()這樣的函數(shù),因為位置在字典中并不重要。。關(guān)聯(lián)才是重要的。。
student.get("k","not found")。字典get函數(shù)的可選參數(shù)。。
'not found'
>>> student.get("k")
****************************
lik9=student.keys()
>>> lik9
dict_keys(['h', 1, 'l', '8'])
>>> type(lik9)
<class 'dict_keys'>也不是列表。。
for i in lik9:
...? ? print(i,end=" ")
...? ? student[i]
*******************
ron={"1":5,"8":9,"5":"k"}
>>> op0=ron.keys()
>>> sorted(op0)
['1', '5', '8']鍵值必須是同一種數(shù)據(jù)類型,才能用sorted()排序。。用op0.sort()是錯誤的。。因為鍵值列表類型不同于列表的新類型。。使用sorted(鍵列表)就成為了真正的列表。。
type(op0);l9=sorted(op0)
<class 'dict_keys'>
>>> l9
['1', '5', '8'],,
字典的鍵不能更改,值可以,
student
{'h': 'p', 1: 'p', 'l': 'p', '8': 'p'}
>>> student["0"]="p"
>>> student
{'h': 'p', 1: 'p', 'l': 'p', '0': 'p', '8': 'p'},,不存在的鍵,自動添加進去。。
del student["0"]
>>> student
{'h': 'p', 1: 'p', 'l': 'p', '8': 'p'},刪掉一組關(guān)聯(lián)。。
字典組建過程中可能碰巧產(chǎn)生排序。字典不強調(diào)位置。如果需要按序號打印,注意使用sorted()對鍵表(新的類型)排序產(chǎn)生由鍵值組成的新真正列表。
for i in range(5):
...? ? print(i,end=" ")
...? ? type(i)
...
0 <class 'int'>
1 <class 'int'>
2 <class 'int'>
3 <class 'int'>
4 <class 'int'>
用str(i)強制轉(zhuǎn)換數(shù)據(jù)為字符串類型。
dict3={4:9}
>>> dict2.update(dict3)
>>> dict2
{1: 5, 4: 9, 5: 6},,注意字典update()的用法,,
temp_list=may_high_temp.values(),values()是值。
temp_list
dict_values([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])
>>> max_temp=max(temp_list)
5.3 Counter.most_common(n)
使用most_common(n)返回一個list, list中包含Counter對象中出現(xiàn)最多前n個元素。
Counter(temp_list)
Counter({1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 16: 1, 17: 1, 18: 1, 19: 1, 20: 1, 21: 1, 22: 1, 23: 1, 24: 1, 25: 1, 26: 1, 27: 1, 28: 1, 29: 1, 30: 1, 31: 1})轉(zhuǎn)換為counter類
使用most_common(n)返回一個list, list中包含Counter對象中出現(xiàn)最多前n個元素。
>>> c = Counter('abracadabra')>>> c
Counter({'a': 5,'b': 2,'r': 2,'d': 1,'c': 1})>>> c.most_common(3)
[('a', 5), ('b', 2), ('r', 2)]
mode_list
[(8, 8)]
>>> Counter(temp_list)
Counter({8: 8, 9: 5, 2: 4, 6: 3, 4: 3, 5: 3, 7: 2, 3: 2, 1: 1})
>>> mode_list[0]
(8, 8)
>>> mode_list[0][1]
8
>>> mode_list[0][0]
8
**********************************
c = Counter('abracadabra')
>>> c
Counter({'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1})
c.most_common(1)
[('a', 5)]從中拿出來數(shù)據(jù)
b=c.most_common(1)
>>> b[0]
('a', 5)
>>> b[0][1]
5
>>> b[0][0]
'a'
>>>
集合,,> jihe1=set()
>>> type(jihe1)
<class 'set'>
jihe1.add("4")
******************
jihe2=set([1,5,6,8])
>>> jihe2
{8, 1, 5, 6}
type({1,2,6})
<class 'set'>
*****************************
{1,2,3}.union({8,9,6})
{1, 2, 3, 6, 8, 9}
{1,2,3,2}.union({1,8,6})
{1, 2, 3, 6, 8},,,并集自動將相同變量置為一個。。
{1,2,3,2}.intersection({1,8,6})交集
{1}
{1,2,3,2}.difference({1,8,6})差集
{2, 3}
******************************
{1,2,3,2}.symmetric_difference({1,8,6})對稱差集
{8, 2, 3, 6}
sorted會將集合變?yōu)榱斜恚暗淖值滏I列表中變?yōu)檎嬲斜怼!?/p>
sorted({1,2,3,6})
[1, 2, 3, 6]。。。
****************************
a={1,2,3,4}
>>> a.update([1,5,6,9])
>>> a
{1, 2, 3, 4, 5, 6, 9}
>>> a.remove(9)
>>> a
{1, 2, 3, 4, 5, 6}
a.discard(4)
>>> a
{1, 2, 3, 5, 6}
discard不拋出異常,而remove會拋出異常。。
update可以一次多個元素,update([1,2,3])而remove只能一個remove(1),,remove([1,2])是錯誤的。。
a={1:2,5:9}
>>> a.pop(1)。。。pop(鍵)關(guān)聯(lián)被刪掉。
2
>>> a
{5: 9}
****************************
{1,2}.issubset({1,2,6,4,8})
True
a={1:5,6:9}
a.clear()清空字典。
a={1:9,8:10,7:6}
>>> a.items()返回鍵值對
dict_items([(8, 10), (1, 9), (7, 6)])
>>> sorted(a.items())
[(1, 9), (7, 6), (8, 10)]
b=sorted(a.items())
>>> b[0]
(1, 9)
unicode格式使用2個字節(jié)存儲字符,比ascii格式容納更多的文本,可以支持多種不同的語言。。
binarys1=b'123456'
>>> print(binarys1)
b'123456'
>>> print(binarys1[0])
49
>>> print(binarys1[1])
50
print(chr(binarys1[1]))
2
**********************
print("\'")
'
************************
> a="this is a "
>>> a[3]
's'
>>> a[4]
' '
*******************
a[3]="d"是不能改變字符串的內(nèi)容的。。
print(a[3:6])
s i
a.replace("i","d")
'thds ds a '
> a.split()以空格分開。。
['this', 'is', 'a']
''.join(["1","2","3"])
'123'
>>> ' '.join(["1","2","3"])
'1 2 3'
'/'.join(["1","2","3"])。。。'分割符號'join([列表])
'1/2/3'
************************************
a=12
>>> a.isdigit()報錯
a="12"
>>> a.isdigit()正確isdigit()是不能直接使用的,,點+digit()才是對的。。用來判斷是否為數(shù)字字符串。
a="this is a girl"
>>> "i" in a
True
"is" in a
True
> a.find("th")
0
>>> a.find("h")
1
a.find("l")
13
>>> a.find("p")沒有改字符的話返回-1
-1
> a.find("i")
2
>>> a.find("i",3,)多處位置存在"i",因為他只返回第一次出現(xiàn)的索引。。
5
a.find("i",6,)
11
a.rfind("i")從右往左查詢"i"..
11
a.index("i")
2
>>> a.index("i",6)
11
a.count("i")
3
"1 2 {}".format(10)
'1 2 10'
"1 2 {} {}".format(10,"j")
'1 2 10 j'
"1 2 3 {dd}".format(dd=89)
'1 2 3 89'
>>> "1 2 3 {dd}".format(78)報錯
"1 2 3 {0}".format(78)正確。。
'1 2 3 78'
"this is pi {}".format(3.1419926)
'this is pi 3.1419926'
"this is pi {0:.2f}".format(3.1419926)
'this is pi 3.14'
"{0:x}".format(56)
'38'
************************************
"{0:b}".format(56)
'111000'
"{0:+}".format(85)負號是不行的。。
'+85'
"{0:.2f}".format(12.365)
'12.37'
"{0:+.2f}".format(12.365)
'+12.37'
>>> "{0:.2f}".format(12.364)
'12.36'
*******************************
"this{0:>10d}".format(12)空出10個空格。。
'this? ? ? ? 12'
"\u0020"為Unicode的Unicode轉(zhuǎn)義編碼。。為空格的編碼。。
' '
> "A".casefold()
'a'
>>> "是".casefold()特殊字符也是可以的,大寫轉(zhuǎn)小寫。。
'是'
caplitalize,, 'this'.capitalize()
'This'
*******************************
"vd".isalnum()
True
>>> "vd1".isalnum()
True
>>> "vd1*".isalnum()
False?
*******************************************
os函數(shù)允許你使用各種操作系統(tǒng)的功能,如創(chuàng)建目錄,os并不是python的內(nèi)建函數(shù),使用需要import os
import os
>>> os.getcwd()
'/home/pi'
os.mkdir('MyNewDir')
>>> os.chdir('MyNewDir')更改為當前工作目錄
>>> os.getcwd()提供當前目錄的絕對目錄引用。。
'/home/pi/MyNewDir'
> os.mkdir('/home/pi/data')創(chuàng)建data文件目錄,,
os.getcwd()
'/home/pi/data'
temp_file=open('2015.txt','w'),,w打開文件寫入,不存在就創(chuàng)建。。
a,r,w,,open的三個可選參數(shù)。。r+,rb+,wb,,,,+號和文件指針有關(guān)。。?
temp_file=open('2015.txt','w')這里沒有使用絕對路徑。。
>>>
KeyboardInterrupt
>>> temp_file.closed
False
temp_file.mode
'w'
temp_file.name不會返回絕對路徑。。
'2015.txt'
os.getcwd()
'/home/pi/data'
os.getcwd()
'/home/pi'
file2=open("/home/pi/data/2015.txt",'r')這里使用了絕對路徑
>>> file2.mode
'r'
file2.name
'/home/pi/data/2015.txt'返回完整路徑
********************************
file2=open("2015.txt","r")
>>> file2.name
'2015.txt'
>>> file2.read()
'this is a girl.\nGit is a control system.\n'
*********************
file3=file2.read()
>>> type(file3)
<class 'str'>讀取返回為字符串類型。。。
> file3=file2.read(),,,,file2.read()使用一次就會不起作用。。。
>>> file3
'this is a girl.\nGit is a control system.\n'
>>> file3[0]
't'。。。。。 file3[0:7]
'this is'
***********************************
file3=file2.readline()
>>> file3[0]
't'
>>> file3
'this is a girl.\n'
*******************************
file2.readline()
'Git is a control system.\n'
>>> file2.readline()
''
********************************
file2=open('2015.txt','r')
>>> for i in range(1,3):
...? ? kk=file2.readline()
...? ? print(kk,end='')
...
this is a girl.
Git is a control system.
*************************************
kk=file2.readline()
>>> kk
'this is a girl.\n'
>>> kk1=file2.readline()
>>> kk1
'Git is a control system.\n'
read()讀取太慢,一次接收所有字符,readline()逐行讀取。。
這里的換行符被end=''干擾就可以避免產(chǎn)生雙倍行距。。。
rstrip
***********************
for i in range(1,3):
...? ? kk=file2.readline()
...? ? kk=kk.rstrip('\n')是刪除掉換行符與上面end=''抑制是不同的。。
...? ? print(kk)
...
this is a girl.
Git is a control system.
*****************
for i in range(2):
...? ? print("122",end='')表示不換行
...? ? print("456")
...
122456
122456
*******************************
for i in range(2):
...? ? print("122")換行了
...? ? print("456")
...
122
456
122
456
***************************
"1223\n".rstrip('\n')
'1223'
.tell()表示當前文件的指針位置。。每個指針表示要讀取的下一個字符。。
file2=open('2015.txt','r')
>>> file2.tell()
0
****************************************
file2.readline();file2.tell()
'this is a girl.\n'
16
最初的文件指針設(shè)置為0,即文件的開頭。。
file2=open('2015.txt','r')
>>> file3=file2.read(2)
>>> file3
'th'
th'
>>>
KeyboardInterrupt
>>> file2.tell()
2
file2.read(2)
'is'
>>> file2.read(2)
' i'
file2.seek(0)
0
>>> file2.read(2)
'th'
read(文件指針)和seek(文件指針)可以指定不按順序讀取文件。。
touch 2016.txt。。。。創(chuàng)建一個空2016.txt文件。。
pi@raspberrypi:~/data $ ls
2015.txt? 2016.txt
******************
echo "sss">>2016.txt直接寫入2016.txt,,單個>會刪掉原文本,>>則會從上往下寫。。
os.listdir()
['2016.txt', '2015.txt']列出當前工作目錄下的文件名。
*******************************
file2=open('2015.txt','r')
>>> for i in file2:
...? ? print(i,end='')
...
this is a girl.
Git is a control system.什么read*()都不用,這種做法直接讀取。。
****************************************************
os.getcwd()
'/home/pi'
>>> os.chdir('/home/pi/data')
>>> os.getcwd()
'/home/pi/data'
os.listdir()
['2016.txt', '2015.txt']
os.chdir('/home/pi/data')
>>> os.listdir()
['2016.txt', '2015.txt']
**********************************************************
文件打開在退出程序之前,一定要關(guān)閉。
file2=open('2015.txt','r')
>>> file2.closed
False
>>> file2.close()
>>> file2.closed
True
文件關(guān)閉可以使寫緩存寫入文本中,不關(guān)閉直接打開另一個的文件的話,寫緩存的內(nèi)容就會丟失。。。
file2=open('2014.txt','w')又寫入了一個2014.txt文件。。
>>> os.listdir()
['2016.txt', '2014.txt', '2015.txt']
如果原來存在2015.txt且有內(nèi)容,file2=open('2015.txt','w'),這將導致原文件內(nèi)容被清空。。寫模式會移除內(nèi)容。。
lstrip()和rstrip()
open('2015.txt','a'),,模式a將文件指針指向文件末尾。。.write方法就會從文件末尾開始寫入數(shù)據(jù)。。文件不會丟失任何數(shù)據(jù)。。
file2.seek(0),,,,,定位文本的指針。。
w+,,,,,,可讀可寫,指針開頭,覆蓋原有內(nèi)容,a+可讀可寫,指針指向尾部,沒有文件的話會創(chuàng)建。。
r+可讀可寫,指針開頭,覆蓋原有內(nèi)容,沒有文件的話,打開失敗。。不會創(chuàng)建文件。。。
.tell()會告訴文本指針的位置。。