python开始之路—基础中的基础
python之路: 基礎(chǔ)篇
| 一、Python |
1、python是怎么來(lái)的
?? ?是在1989年吉多·范羅蘇姆,在圣誕節(jié)的時(shí)候閑著無(wú)聊自己用C語(yǔ)言開(kāi)發(fā)的,一個(gè)腳本解釋程序,作為ABC語(yǔ)言的一種繼承。 ?
2、有哪些公司在用?? ?
?? ?Youtube、Dropbox、BT、Quora(中國(guó)知乎)、豆瓣、知乎、Google、Yahoo!、Facebook、NASA、百度、騰訊、汽車(chē)之家、美團(tuán)等。互聯(lián)網(wǎng)公司廣泛使用Python來(lái)做的事
3、python作用
?? ?數(shù)據(jù)分析、組件集成、網(wǎng)絡(luò)服務(wù)、圖像處理、數(shù)值計(jì)算和科學(xué)計(jì)算等眾多領(lǐng)域。
4、有哪些版本
?? ?Cpython(基于C語(yǔ)言實(shí)現(xiàn))、Jyhton(基于java語(yǔ)言的實(shí)現(xiàn))、IronPython(基于C#語(yǔ)言的實(shí)現(xiàn))、PyPy(比較特殊基于python實(shí)現(xiàn))其他的有RubyPython(基于ruby語(yǔ)言的實(shí)現(xiàn))、Brython
5、與其他語(yǔ)言的優(yōu)缺點(diǎn)與其他語(yǔ)言的優(yōu)缺點(diǎn)
?? ?C、Java、php、python
5-1、在速度上的區(qū)別:
C語(yǔ)言直接編譯成機(jī)器語(yǔ)言,所以比較快。python需要編譯成字節(jié)碼,在從字符語(yǔ)言編譯成機(jī)器語(yǔ)言,所以運(yùn)行速度比
較慢。其他語(yǔ)言也是一樣,相比python都需要先轉(zhuǎn)成字符語(yǔ)言在編譯成機(jī)器語(yǔ)言,方可使用。
5-2、功能上
更多功能和類庫(kù)(內(nèi)置、開(kāi)源、自己寫(xiě))
6、中文解釋碼
?-*-?coding:utf-8?-*- ?
coding:utf-87、為了不用python 腳本的方式需要編碼聲明
注釋:“#”單行注釋,多行注釋“""" """”
模塊:?
1、import sys
使用了argv模塊,sys.argv 用來(lái)捕獲執(zhí)行執(zhí)行python腳本時(shí)傳入的參數(shù),倒入之后會(huì)生成pyc,python執(zhí)行主要看文件的和pyc是否一樣如果一樣優(yōu)先級(jí)是pyc,不一樣是文件
2、import getpass模塊 輸入密碼不顯示,隱藏所輸入的內(nèi)容
| 二、變量 |
變量就是在內(nèi)存中開(kāi)辟一塊空間并給這塊空間賦一個(gè)名稱就是變量名
變量的作用:昵稱,其代指內(nèi)存里某個(gè)地址中保存的內(nèi)容
字節(jié)碼:
1、變量規(guī)則
?? ?1、變量名:大小寫(xiě)字母、數(shù)字、下劃線 數(shù)字不能作為起始 不能作為python內(nèi)置的關(guān)鍵字
?? ?2、這些關(guān)鍵詞不能聲明變量名['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
?? ?3、變量的第一個(gè)字符不能是數(shù)字
? ? 2、流程控制和縮進(jìn)
?? ?外層變量,可以被內(nèi)層變量使用
?? ?內(nèi)層變量,無(wú)法被外層變量使用
?? ?在字符串里,name1 = ”字符串“ name2 = name1
?? ?name1改變,name2不變.字符串的特性一旦修改,重新創(chuàng)建。賦同一個(gè)值給兩個(gè)不同的變量就是開(kāi)辟兩個(gè)不同的空間,
只是python的優(yōu)化把兩個(gè)變量值和在一起了,如果超過(guò)這個(gè)賦值池,就會(huì)開(kāi)辟兩塊空間體現(xiàn)。數(shù)字池是-5到257
python執(zhí)行過(guò)程:加載內(nèi)存、此法分析、語(yǔ)法分析、編譯---》》 字節(jié)碼
語(yǔ)法:
輸入輸出
輸入:raw_input
輸出:print
輸入不顯示:getpass模塊
| 三、循環(huán) |
"=="是兩個(gè)值之間的對(duì)比,
流程控制
if???else:
| 四、數(shù)值 |
兩部分:
1、單值?
1、單值????????數(shù)字
????????????整型(int)(32位:取值范圍為-2**31~2**31-1,即-2147483648~2147483647。64位,取值范圍為-2**63~2**63-1,即-9223372036854775808~9223372036854775807)
????????????長(zhǎng)整型(long)(Python沒(méi)有限制長(zhǎng)整數(shù)數(shù)值的大小)
????????????浮點(diǎn)型(float)(浮點(diǎn)數(shù)用來(lái)處理實(shí)數(shù),即帶有小數(shù)的數(shù)字)
????????????復(fù)數(shù)(complex)( 復(fù)數(shù)由實(shí)數(shù)部分和虛數(shù)部分組成,一般形式為x+yj,其中的x是復(fù)數(shù)的實(shí)數(shù)部分,y是復(fù)數(shù)的虛數(shù)部分,這里的x和y都是實(shí)數(shù)。注:Python中存在小數(shù)字池:-5?~?257)
????????????注:python中存在小數(shù)字池:-5~257
????????布爾值(bool)????True、false????1?0
????????字符串(str)
????2、集合
????????列表
????????元祖
????????字典
????????哈希表 >>>?name?=?"i?am?%s?%d"????%("alex",13)
或
>>>?name?=?"i?am?%s?%d"????#占位符%s、%d
>>>?name?%("wulaoer",13)????#按照占位符的順序
'i?am?alex?13'
##################################################################################################
>>>?name?=?"i?am?{0},age{1}"
>>>?name.format("wulaoer",14)
'i?am?wulaoer,age14'? >>>?name?=?"wulaoer"
>>>?print?name[0:]????????#下標(biāo)識(shí)從0開(kāi)始的
wulaoer >>>?print?name[0:2]????????#最后一個(gè)不顯示
wu
>>>?print?name[-1]????????#最后一個(gè)
r
>>>?print?name[:-1]????????#從最后一個(gè)開(kāi)始
wulaoe >>>?print?name[:-2]????????#左邊顯示右邊不顯示
wu >>>?print?len(name)????????#多少個(gè)字符
6? 1?>>>?name?=?"wulaoer"
?2?>>>?print?name
?3????wulaoer
?4?>>>?print?name.lstrip()????????#去掉左邊空格
?5?wulaoer
?6?>>>?name?=?"wulaoer"????
?7?>>>?print?name.rstrip()????????#去掉右邊空格
?8?????wulaoer
?9?>>>?print?name.strip()????????#去掉兩邊的空格
10?wulaoer? 1?>>>?shopping_list?=?['Iphone',?'Mac',?'Bike','Coffee','Car','Clothes','Food','Gi
2?ft']
3?>>>?shopping_list
4?['Iphone',?'Mac',?'Bike',?'Coffee',?'Car',?'Clothes',?'Food',?'Gift']
5?>>>?len(shopping_list)
6?8 1?>>>?shopping_list?=?['Iphone',?'Mac',?'Bike','Coffee','Car','Clothes','Food','Gift']????#創(chuàng)建一個(gè)列表
2?>>>?shopping_list
3?['Iphone',?'Mac',?'Bike',?'Coffee',?'Car',?'Clothes',?'Food',?'Gift']????????#顯示列表
4?>>>?shopping_list[0]????????#輸出列表的第一個(gè)元素
5?'Iphone'
6?>>>?shopping_list[2]????????#輸出列表的第二個(gè)元素,是從0開(kāi)始的
7?'Bike' ?1?>>>?name?=?['wulaoer','hello','word']
?2?>>>?name_list?=?['wulaoer','hello','word']
?3?>>>?name_list
?4?['wulaoer',?'hello',?'word']
?5?>>>?name_list.append('day')????????#添加
?6?>>>?name_list
?7?['wulaoer',?'hello',?'word',?'day']
?8?=============================================================
?9?>>>?del?name_list[0]????????#刪除,
10?>>>?del?name_list[0:3]????????#刪除索引0到3,不包括3
11?或
12?>>>?name_list
13?['wulaoer',?'hello',?'word',?'day']
14?>>>?name_list.remove('wulaoer')????????#刪除wulaoer,如果有多個(gè)wulaoer,那會(huì)刪除從左邊開(kāi)始第一個(gè)wulaoer。
15?>>>?name_list
16?['hello',?'word',?'day']
17?=============================================================
18?>>>?name_list.insert(2,"wulaoer")????????#插入
19?>>>?name_list
20?['hello',?'word',?'wulaoer',?'day']
21?>>>?name_list[3]?=?'world'????????????#修改
22?>>>?name_list
23?['wulaoer',?'hello',?'wulaoer',?'world']
24?========================================================
25?>>>?'_'.join(name_list)????????????????#連接根據(jù)單引號(hào)的符號(hào)連接兩個(gè)元素之間的
26?'wulaoer_hello_wulaoer_world'
?列表的增刪改:只是對(duì)于列表的最小單元
1?'_'.join(列表)?2?>>>?name_list
?3?['wulaoer',?'hello',?'wulaoer',?'world']
?4?>>>?name_list.index('wulaoer')????????????#返回列表的索引值,如果有多個(gè)返回第一個(gè)值
?5?0
?6?>>>?name_list.index('world')
?7?3
?8?=============================================================
?9?>>>?name_list.count('wulaoer')????????????#統(tǒng)計(jì)索引值
10?2
11?>>>?name_list.count('world')
12?1
13?>>>?name_city?=?['henan',?'hebei',?'wubei']
14?>>>?name_list.extend(name_city)????????????#合并兩個(gè)表
15?>>>?name_list
16?['wulaoer',?'hello',?'wulaoer',?'world',?'henan',?'hebei',?'wubei']
17?=============================================================
18?>>>?name_list
19?['wulaoer',?'hello',?'wulaoer',?'world',?'henan',?'hebei',?'wubei']
20?>>>?name_list.sort()????????#將列表排序,根據(jù)26個(gè)字符的順序
21?>>>?name_list
22?['wulaoer',?'wulaoer',?'hebei',?'hello',?'henan',?'world',?'wubei']
23?=============================================================
24?>>>?name_list.reverse()????????#將列表反轉(zhuǎn)排序
25?>>>?name_list
26?['wubei',?'world',?'henan',?'hello',?'hebei',?'wulaoer',?'wulaoer']
27?=============================================================
28?>>>?for?i?in?name_list:????????#遍歷列表
29?...?????print?i
30?...
31?wubei
32?world
33?henan
34?hello
35?hebei
36?wulaoer
37?wulaoer
| 五、元組(tuple) |
元組是另外一種序列表,一旦初始化不允許修改,在表示只有一個(gè)元素的元組里,也要價(jià)格“,”避免誤解成數(shù)學(xué)計(jì)算意義
注:元祖不可修改,列表可以修改
1、循環(huán)
2?????continue????#本次循環(huán)不再繼續(xù)
3??????
4?????break????????????#跳出循環(huán)
5?while????條件?
| 六、字典(Dict) |
2?person.values()????????#所有values
3?person.items()????????#所有元素,僅for循環(huán)時(shí),使用,將元素賦值給keys
4?person.items()
5?for?k,v?in?person.items()
6?????print?k
7?????print?v
8?????print?'============='? ?1?>>>?class_name?=?{
?2?????'name':'toy',?
?3?????'job':'IT',?
?4?????'age':12,
?5?}
?6?或者
?7?>>>?class_name?=?{'name':'toy',?'job':'IT',?'age':12,}
?8?>>>?class_name
?9?{'age':?12,?'job':?'IT',?'name':?'toy'}
10?=============================================================
11?>>>?class_name['age']????#查看key為age的value
12?12
13?>>>?class_name['age']?=?19????#將key的value改為“19”
14?>>>?class_name
15?{'age':?19,?'job':?'IT',?'name':?'toy'}
16?=============================================================
17?>>>?class_name['company']?=?'AUTOHOME'????#在dict中查找key為“company”,將其改成“AUTOHOME”,如果沒(méi)有就創(chuàng)建一條
18?>>>?class_name
19?{'age':?19,?'job':?'IT',?'company':?'AUTOHOME',?'name':?'toy'}
20?=============================================================
21?>>>?class_name.pop('company')????#刪除key為“company”的數(shù)據(jù)
22?'AUTOHOME'
23?>>>?class_name
24?{'age':?19,?'job':?'IT',?'name':?'toy'}
25?>>>?class_name.popitem(?)????????#隨機(jī)刪除一條數(shù)據(jù),dict為空時(shí)用此法會(huì)報(bào)錯(cuò)
26?('age',?19)
27?>>>?class_name
28?{'job':?'IT',?'name':?'toy'}
29?>>>?class_name.items()????????????#將dict的key和value轉(zhuǎn)換成列表的形式顯示
30?[('job',?'IT'),?('name',?'toy')]
31?=============================================================
32?>>>?class_name['age']????????#查找一個(gè)不存在的key
33?Traceback?(most?recent?call?last):
34???File?"<stdin>",?line?1,?in?<module>
35?KeyError:?'age'
36?>>>?class_name.has_key('name')????????#判斷dict里是否有一個(gè)’name‘的key,有True,否則False
37?True
38?>>>?class_name.has_key('gae')
39?False
40?>>>?class_name.get('age')????????#查找key,如果存在則返回其value,否則None
41?>>>?class_name.get('name')
42?'toy'
43?=============================================================
44?>>>?class_name
45?{'job':?'IT',?'name':?'toy'}
46?>>>?class_name.clear()????????????????#清空dict
47?>>>?class_name
48?{}
49?=============================================================
50?>>>?class_name?=class_name.fromkeys(['a',?'b',?'c',?'d'],'helo')????????#根據(jù)列表來(lái)創(chuàng)建dict里的key,后面的“helo”是默認(rèn)的value,如果不指定的話則為None
51?>>>?class_name
52?{'a':?'helo',?'c':?'helo',?'b':?'helo',?'d':?'helo'}
53?>>>?class_name.setdefault('e','helo')????#找一個(gè)key為’e‘的記錄,如果不存在,就重新穿件key為“e”,vlaue為“helo”,如果存在直接返回這個(gè)key的value
54?'helo'
55?>>>?class_name.setdefault('c','h')????????#dict的key里有“c”,直接返回“c”的value。
56?'helo'
57?>>>?class_name
58?{'a':?'helo',?'c':?'helo',?'b':?'helo',?'e':?'helo',?'d':?'helo'}
59?=============================================================
60?>>>?dict2?=?{'f':'test',?'g':'test',?'h':'test'}????????#創(chuàng)建一個(gè)新的dict
61?>>>?class_name.update(dict2)????????#用新的dict去更新class_name,如果dict中的key值和class_name中的key值沖突,dict的key值會(huì)覆蓋掉class_name的key值,不存在會(huì)創(chuàng)建相應(yīng)記錄
62?>>>?class_name
63?{'a':?'helo',?'c':?'helo',?'b':?'helo',?'e':?'helo',?'d':?'helo',?'g':?'test',?'f':?'test',?'h':?'test'}?
| 七、遍歷 dict |
?2?class_name?=?{
?3?????????'name':?'tom',
?4?????????'age':?19,
?5?????????'job':?'IT'
?6?}
?7?for?item?in?class_name:
?8?????????print?item,class_name[item]??????#打印key和value
?9?輸出key和value
10?job?IT
11?age?19
12?name?tom
13?或者
14?for?key,val?in?class_name.items():????????#同時(shí)打印key和value
15?????????print?key,val
16?=============================================================
17?for?item?in?class_name:????????
18?????????print?item????????#只會(huì)打印key
19?job
20?age
21?name ? ?
| 八、深淺 copy |
?2?...?????0023:{'name':'tom',?'age':18,?'job':'IT',?'phone':3456},
?3?...?????3951:{'name':'Bob',?'age':19,?'job':'PHP',?'phone':5563},
?4?...?????5342:{'name':'Com',?'age':20,?'job':'IOS',?'phone':2942},
?5?...?}
?6?>>>?staff_contacts
?7?{19:?{'job':?'IT',?'phone':?3456,?'age':?18,?'name':?'tom'},?5342:?{'job':?'IOS'
?8?,?'phone':?2942,?'age':?20,?'name':?'Com'},?3951:?{'job':?'PHP',?'phone':?5563,
?9?'age':?19,?'name':?'Bob'}}????????
10?=============================================================
11?>>>?contacts2?=?staff_contacts
12?>>>?staff_contacts[0023]['age']?=?38
13?>>>?print?'staff_contacts:',staff_contacts[0023]
14?staff_contacts:?{'job':?'IT',?'phone':?3456,?'age':?38,?'name':?'tom'}
15?>>>?print?'contacts2:',?contacts2[0023]
16?contacts2:?{'job':?'IT',?'phone':?3456,?'age':?38,?'name':?'tom'}
17?>>>?print?'id?of?staff_contacts:',id(staff_contacts[0023])????#查看兩個(gè)dict的內(nèi)存地址
18?id?of?staff_contacts:?35454488
19?>>>?print?'id?of?contacts2:',id(contacts2[0023])
20?id?of?contacts2:?35454488? 1?>>>?contacts2?=?staff_contacts.copy()
?2?用幫助來(lái)查看這個(gè)語(yǔ)法的解釋:
?3?>>>?help(staff_contacts.copy)
?4?Help?on?built-in?function?copy:
?5?copy(...)
?6?????D.copy()?->?a?shallow?copy?of?D
?7?a?shallow?copy就是淺copy的意思
?8?>>>?staff_contacts?=?{
?9?...
10?...??????0023:{'name':'wulaoer','age':29,'department':'IT','phone':3423},
11?...?????3333:'nothing'
12?...?}????????????????#重新建一個(gè)dict
13?>>>?contacts2?=?staff_contacts.copy()?#淺copy
14?>>>?staff_contacts[0023]['age']?=?28????#修改dict的兩個(gè)值
15?>>>?staff_contacts[3333]?=?'change?this?by?original?dict'
16?>>>?contacts2
17?{19:?{'department':?'IT',?'phone':?3423,?'age':?28,?'name':?'wulaoer'},?3333:?'n
18?othing'}
19?>>>?staff_contacts
20?{19:?{'department':?'IT',?'phone':?3423,?'age':?28,?'name':?'wulaoer'},?3333:?'c
21?hange?this?by?original?dict'}
22?#注經(jīng)過(guò)淺copy之后,3333已經(jīng)是兩個(gè)數(shù)據(jù)已經(jīng)獨(dú)立了,而age還是共享內(nèi)存數(shù)據(jù),這個(gè)就是淺copy?
| 九、深 copy |
2?>>>?contacts2?=?copy.deepcopy(staff_contacts)
3?>>>?contacts2
4?{19:?{'department':?'IT',?'phone':?3423,?'age':?28,?'name':?'wulaoer'},?3333:?'c
5?hange?this?by?original?dict'}
6?>>>?staff_contacts
7?{19:?{'department':?'IT',?'phone':?3423,?'age':?28,?'name':?'wulaoer'},?3333:?'c
8?hange?this?by?original?dict'}
9?#深copy之后所有的值都會(huì)copy過(guò)來(lái)。?
| 十、集合 set |
?2?>>>?t?=?set("Hello")????????#字符集合
?3?集合是無(wú)序的,與列表和元祖不同,也不能數(shù)字進(jìn)行索引,集合的元素不能重復(fù)
?4?>>>?t
?5?set(['H',?'e',?'l',?'o'])????#這里只出現(xiàn)了一個(gè)“I”
?6?集合支持一系列標(biāo)準(zhǔn)操作,包括并集、交集、差集和對(duì)稱差集,
?7?>>>?a?=?t?|?s????????#t和s的并集
?8?>>>?a
?9?set([3,?'e',?5,?'H',?10,?'l',?'o',?9])
10?>>>?b?=?t?&?s????????#t和s的交集
11?>>>?b
12?set([])
13?>>>?c?=?t?-s????????#求差集(項(xiàng)在t中,但不在s中)
14?>>>?c
15?set(['H',?'e',?'l',?'o'])
16?>>>?d?=?t?^?s????????#對(duì)稱差集(項(xiàng)在t或s中,但不會(huì)同時(shí)出現(xiàn)在二者中)
17?>>>?d
18?set([3,?'e',?5,?9,?10,?'l',?'o',?'H'])
19?>>>?t.add('x')????????#添加
20?>>>?t
21?set(['H',?'e',?'l',?'o',?'x'])
22?>>>?s.update([11,12,13])????#添加多項(xiàng)
23?>>>?t.remove('H')????????????#刪除,如果不存在報(bào)錯(cuò)KeyError
24?>>>?t
25?set(['e',?'l',?'o',?'x'])????
26?>>>?len(s)????????set的長(zhǎng)度
27?7
28?#######################################################
29?x?in?s????#測(cè)試x是否是s的成員??????????????????????????????#????????????????????????
30?x?not?in?s?????#測(cè)試x是否不是s的成員??????????????????????#
31?#######################################################????
32?>>>?s.discard(5)????#如果在set?“s”中存在元素5,則刪除
33?>>>?s
34?set([9,?10,?11,?12,?13])????
35?>>>?s.pop()????????????#刪除并且返回set?“s”中一個(gè)不確定的元素,如果為空則報(bào)錯(cuò)KeyError
36?9
37?>>>?s
38?set([10,?11,?12,?13])????
39?>>>?s.clear()????????#刪除set?“s”中的所有元素
40?>>>?s
41?set([])????
42?>>>?s.issubset(t)????s<=?t?#測(cè)試是否s中的每個(gè)元素都在t中
43?True
44?>>>?s.issuperset(t)????s>=?t??#測(cè)試是否t中的每個(gè)元素都在s中
45?False????
46?>>>?s.union(t)????????????#返回一個(gè)新的set包含s和t中的每一個(gè)元素
47?set(['x',?'e',?'l',?'o'])
48?>>>?s?|?t
49?set(['x',?'e',?'l',?'o'])????
50?>>>?s.intersection(t)????#返回一個(gè)新的set包含s和t中的公共元素
51?set([])
52?>>>?s&t
53?set([])????
54?>>>?s.difference(t)????????#返回一個(gè)新的set包含s中有但是t中沒(méi)有的元素
55?set([])
56?>>>?s?-?t
57?set([])????
58?>>>?s.symmetric_difference(t)????#返回一個(gè)新的set包含s和t中不重復(fù)的元素
59?set(['x',?'e',?'l',?'o'])
60?>>>?s?^?t
61?set(['x',?'e',?'l',?'o'])????
62?????>>>?s.copy()????????????#返回set?“s”的一個(gè)淺復(fù)制
63?set([])?
| 十一、文件操作 |
?2?標(biāo)識(shí)符'r'表示讀,這樣,就打開(kāi)了一個(gè)文件,如果不存在就提示
?3?>>>?f?=?open('test.tx',?'r')????#不存在有一個(gè)IOError錯(cuò)誤說(shuō)不存在這個(gè)文件
?4?Traceback?(most?recent?call?last):
?5???File?"<stdin>",?line?1,?in?<module>
?6?IOError:?[Errno?2]?No?such?file?or?directory:?'test.tx'
?7?>>>?f.read()????#正確打開(kāi)后要掉用read()方法可以一次讀取文件所有內(nèi)容,python把內(nèi)容讀到內(nèi)存,
?8?'hello?word'
?9?>>>?f.close()????#是關(guān)閉文件,使用完畢后必須關(guān)閉文件,因?yàn)槲募?duì)像會(huì)占用操作系統(tǒng)的資源,并且操作系統(tǒng)在同一時(shí)間打開(kāi)的文件數(shù)量是有限的:
10?如果總是忘記關(guān)閉文件可以使用python引用的with語(yǔ)句來(lái)自動(dòng)調(diào)用close()
11??with?open('path/to/file',)?as?f:
12??????print?f.read()? 1?for?line?in?f.readlines():
2?????print(line.strip())?#把末尾的‘\n’刪除 ? #-*-coding:utf-8-*-
f?=?file('MyNewfile.txt,''w')??#'w'代表以寫(xiě)模式創(chuàng)建一個(gè)新文件
f.write('This?is?frist?line\n')????#往文件里寫(xiě)內(nèi)容
f.write('second?line\n')
f.close()
#上面的寫(xiě)是指重新建一個(gè)文件,如果有MyNewfile.txt這個(gè)文件會(huì)把原來(lái)的文件沖掉? 1?f?=?file('MyNewfile.txt','a')????????#追加模式打開(kāi)文件,如果a追加的文件不存在就會(huì)新建一個(gè)
2?f.write('This?is?third?line,added?in?append?mode\n')
3?f.close()? 1?file_obj.read()????????#將所有讀入內(nèi)存
2?file_obj.readlines()????[行,]?#以列表的形式顯示出來(lái)
3?for?line?in?file_obj.xreadlines():
4?????print?line
5??????
6?for?line?in?file_obj:
7?????#每次循環(huán),只讀一行?
1?file_obj.close()?
總結(jié)
以上是生活随笔為你收集整理的python开始之路—基础中的基础的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 网站搜索功能怎么实现_电商网站上的搜索功
- 下一篇: 利用python转换图片格式