Python文件操作

1.文件的读写

1.1.文件的创建与写入

  • 利用内置函数open获取文件对象

  • 文件操作的模式之写入

  • 文件对象的操作方法之写入保存

<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> # coding:utf<span style="color:#880000">-8</span>
 <span style="color:#880000">2</span> 
 <span style="color:#880000">3</span> import os
 <span style="color:#880000">4</span> 
 <span style="color:#880000">5</span> current_path=os.getcwd()
 <span style="color:#880000">6</span> file_path=os.path.<span style="color:#333333"><strong>join</strong></span>(current_path,<span style="color:#880000">'test_os.txt'</span>)
 <span style="color:#880000">7</span> 
 <span style="color:#880000">8</span> f=<span style="color:#333333"><strong>open</strong></span>(file_path,<span style="color:#880000">'w'</span>,<span style="color:#333333"><strong>encoding</strong></span>=<span style="color:#880000">"utf-8"</span>)  #Windows系统在写入中文时需要设置编码格式
 <span style="color:#880000">9</span> f.write(<span style="color:#880000">"Hello Python;你好 Python"</span>)
<span style="color:#880000">10</span> #f.<span style="color:#333333"><strong>read</strong></span>() io.UnsupportedOperation: not readable,控制台报错,因为f是w模式
<span style="color:#880000">11</span> f.<span style="color:#333333"><strong>close</strong></span>()
<span style="color:#880000">12</span> 
<span style="color:#880000">13</span> f=<span style="color:#333333"><strong>open</strong></span>(file_path,<span style="color:#880000">'w+'</span>,<span style="color:#333333"><strong>encoding</strong></span>=<span style="color:#880000">"utf-8"</span>)  #Windows系统在写入中文时需要设置编码格式
<span style="color:#880000">14</span> f.write(<span style="color:#880000">"Hello World;你好 世界"</span>)
<span style="color:#880000">15</span> print(f.<span style="color:#333333"><strong>read</strong></span>()) #无内容输出,<span style="color:#333333"><strong>read</strong></span>读取文件的话  是从最后一个角标读取
<span style="color:#880000">16</span> f.<span style="color:#333333"><strong>seek</strong></span>(<span style="color:#880000">0</span>)       #此时调用<span style="color:#333333"><strong>seek</strong></span>,选择从索引开始读取,则会读取所有刚才写入的字符串
<span style="color:#880000">17</span> print(f.<span style="color:#333333"><strong>read</strong></span>()) #Hello World;你好 世界
<span style="color:#880000">18</span> f.<span style="color:#333333"><strong>close</strong></span>()
<span style="color:#880000">19</span> 
<span style="color:#880000">20</span> f=<span style="color:#333333"><strong>open</strong></span>(file_path,<span style="color:#880000">'ab'</span>)#a:追加,b:比特类型
<span style="color:#880000">21</span> message=<span style="color:#880000">"python很有意思"</span>
<span style="color:#880000">22</span> #message=b<span style="color:#880000">"python很有意思"</span>   SyntaxError: bytes can only contain ASCII literal characters.之前的知识,我们使用b对字符串内容转译成比特类型报错,因为b特类型不能有汉字,所以得采用其他办法
<span style="color:#880000">23</span> _message=message.encode(<span style="color:#880000">"utf-8"</span>)    #将字符串变成比特类型
<span style="color:#880000">24</span> f.write(_message)
<span style="color:#880000">25</span> f.<span style="color:#333333"><strong>close</strong></span>()
<span style="color:#880000">26</span> 
<span style="color:#880000">27</span> f=<span style="color:#333333"><strong>open</strong></span>(file_path,<span style="color:#880000">'a'</span>)
<span style="color:#880000">28</span> message=(<span style="color:#880000">"1\n"</span>,<span style="color:#880000">"2\n"</span>,<span style="color:#880000">"3\n"</span>)
<span style="color:#880000">29</span> f.writelines(message)
<span style="color:#880000">30</span> f.<span style="color:#333333"><strong>close</strong></span>()
</span></span></span></span>
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> <span style="color:#888888"># coding:utf-8</span>
 <span style="color:#880000">2</span> <span style="color:#333333"><strong>import</strong></span> os
 <span style="color:#880000">3</span> 
 <span style="color:#880000">4</span> <span style="color:#888888">#练习1:自动创建python包</span>
 <span style="color:#880000">5</span> <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>creat_package</strong></span>(path):
 <span style="color:#880000">6</span>     <span style="color:#333333"><strong>if</strong></span> os.path.exists(path):
 <span style="color:#880000">7</span>         <span style="color:#333333"><strong>raise</strong></span> Exception(<span style="color:#880000">'%s 已经存在,不可创建'</span>% path)
 <span style="color:#880000">8</span>     os.makedirs(path)
 <span style="color:#880000">9</span>     init_path=os.path.join(path,<span style="color:#880000">'__init__.py'</span>)
<span style="color:#880000">10</span>     f=open(init_path,<span style="color:#880000">'w'</span>)
<span style="color:#880000">11</span>     f.write(<span style="color:#880000">'# coding:utf-8\n'</span>)
<span style="color:#880000">12</span>     f.close()
<span style="color:#880000">13</span> 
<span style="color:#880000">14</span> <span style="color:#333333"><strong>if</strong></span> __name__==<span style="color:#880000">'__main__'</span>:
<span style="color:#880000">15</span>     current_path=os.getcwd()
<span style="color:#880000">16</span>     path=os.path.join(current_path,<span style="color:#880000">'test1'</span>)
<span style="color:#880000">17</span>     creat_package(path)
</span></span></span></span>
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> <span style="color:#888888"># coding:utf-8</span>
 <span style="color:#880000">2</span> <span style="color:#333333"><strong>import</strong></span> os
 <span style="color:#880000">3</span> 
 <span style="color:#880000">4</span> <span style="color:#888888">#练习2:写入文件</span>
 <span style="color:#880000">5</span> <span style="color:#333333"><strong>class</strong></span> <span style="color:#880000"><strong>Open</strong></span>(object):
 <span style="color:#880000">6</span>     <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>__init__</strong></span>(self,path,mode=<span style="color:#880000">'w'</span>,is_return=True):
 <span style="color:#880000">7</span>         self.path=path
 <span style="color:#880000">8</span>         self.mode=mode
 <span style="color:#880000">9</span>         self.is_return=is_return
<span style="color:#880000">10</span> 
<span style="color:#880000">11</span>     <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>write</strong></span>(self,message):
<span style="color:#880000">12</span>         f=open(self.path,mode=self.mode)
<span style="color:#880000">13</span>         <span style="color:#333333"><strong>if</strong></span> self.is_return:
<span style="color:#880000">14</span>             message= <span style="color:#880000">'%s\n'</span> %message
<span style="color:#880000">15</span>         f.write(message)
<span style="color:#880000">16</span>         f.close()
<span style="color:#880000">17</span> 
<span style="color:#880000">18</span> <span style="color:#333333"><strong>if</strong></span> __name__==<span style="color:#880000">'__main__'</span>:
<span style="color:#880000">19</span>     current_path=os.getcwd()
<span style="color:#880000">20</span>     open_path=os.path.join(current_path,<span style="color:#880000">'a.txt'</span>)
<span style="color:#880000">21</span>     o=Open(open_path)
<span style="color:#880000">22</span>     o.write(<span style="color:#880000">"Hello World"</span>)
</span></span></span></span>
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> <span style="color:#888888"># coding:utf-8</span>
 <span style="color:#880000">2</span> <span style="color:#333333"><strong>import</strong></span> os
 <span style="color:#880000">3</span> 
 <span style="color:#880000">4</span> <span style="color:#888888">#练习2:容错处理完善</span>
 <span style="color:#880000">5</span> <span style="color:#333333"><strong>class</strong></span> <span style="color:#880000"><strong>Open</strong></span>(object):
 <span style="color:#880000">6</span>     <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>__init__</strong></span>(self,path,mode=<span style="color:#880000">'w'</span>,is_return=True):
 <span style="color:#880000">7</span>         self.path=path
 <span style="color:#880000">8</span>         self.mode=mode
 <span style="color:#880000">9</span>         self.is_return=is_return
<span style="color:#880000">10</span> 
<span style="color:#880000">11</span>     <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>write</strong></span>(self,message):
<span style="color:#880000">12</span>         f=open(self.path,mode=self.mode)
<span style="color:#880000">13</span>         <span style="color:#333333"><strong>if</strong></span> message.endswith(<span style="color:#880000">"\n"</span>):
<span style="color:#880000">14</span>             self.is_return=<span style="color:#333333"><strong>False</strong></span>
<span style="color:#880000">15</span>             <span style="color:#333333"><strong>if</strong></span> self.is_return:
<span style="color:#880000">16</span>                 message= <span style="color:#880000">'%s\n'</span> %message
<span style="color:#880000">17</span>         <span style="color:#333333"><strong>try</strong></span>:
<span style="color:#880000">18</span>             f.write(message)
<span style="color:#880000">19</span>         <span style="color:#333333"><strong>finally</strong></span>:
<span style="color:#880000">20</span>             f.close()
<span style="color:#880000">21</span> 
<span style="color:#880000">22</span> <span style="color:#333333"><strong>if</strong></span> __name__==<span style="color:#880000">'__main__'</span>:
<span style="color:#880000">23</span>     current_path=os.getcwd()
<span style="color:#880000">24</span>     open_path=os.path.join(current_path,<span style="color:#880000">'a.txt'</span>)
<span style="color:#880000">25</span>     o=Open(open_path,<span style="color:#880000">'a'</span>)
<span style="color:#880000">26</span>     o.write(<span style="color:#880000">"Hello World\n"</span>)
</span></span></span></span>

1.2.文件的读操作

  • 文件操作的模式之读

r:返回数据类型为字符串;rb:读取位类型

  • 文件对象的操作方法之读取

<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> <span style="color:#888888"># coding:utf-8</span>
 <span style="color:#880000">2</span> 
 <span style="color:#880000">3</span> <span style="color:#333333"><strong>import</strong></span> os
 <span style="color:#880000">4</span> 
 <span style="color:#880000">5</span> file_path=os.getcwd()
 <span style="color:#880000">6</span> file=os.path.join(file_path,<span style="color:#880000">'a.txt'</span>)
 <span style="color:#880000">7</span> 
 <span style="color:#880000">8</span> <span style="color:#888888">#read用法,读取文件所有内容,返回的是字符串,read可以和seek配合使用,重置角标去取</span>
 <span style="color:#880000">9</span> f=open(file,<span style="color:#880000">'r'</span>)
<span style="color:#880000">10</span> <span style="color:#888888"># data=f.read()</span>
<span style="color:#880000">11</span> <span style="color:#888888"># print(data)</span>
<span style="color:#880000">12</span> <span style="color:#880000">'''
13 Hello World
14 Hello World
15 Hello World
16 '''</span>
<span style="color:#880000">17</span> 
<span style="color:#880000">18</span> <span style="color:#888888">#readlines用法,返回的是一个列表,每个元素带\n换行</span>
<span style="color:#880000">19</span> <span style="color:#880000">'''
20 data=f.readlines()
21 print(data)     #['Hello World\n', 'Hello World\n', 'Hello World\n']
22 _data=[]
23 for i in data:
24     temp=i.strip()  #strip默认去除空格,换行符等
25     if temp != '':  #去除空行
26         _data.append(temp)
27 print(_data)    #['Hello World', 'Hello World', 'Hello World']
28 '''</span>
<span style="color:#880000">29</span> 
<span style="color:#880000">30</span> <span style="color:#888888">#readline用法,逐行去取</span>
<span style="color:#880000">31</span> <span style="color:#880000">'''
32 data=f.readline()
33 print(data)     #Hello World
34 data=f.readline()
35 print(data)     #123
36 '''</span>
<span style="color:#880000">37</span> 
<span style="color:#880000">38</span> <span style="color:#888888">#mode用法,使用什么方式打开open(path,mode)文件,返回mode的值</span>
<span style="color:#880000">39</span> print(f.mode)   <span style="color:#888888">#r</span>
<span style="color:#880000">40</span> 
<span style="color:#880000">41</span> <span style="color:#888888">#name用法,返回文件名称</span>
<span style="color:#880000">42</span> print(f.name)                       <span style="color:#888888">#D:\WorkSpace\Python_Study\a.txt</span>
<span style="color:#880000">43</span> print(os.path.split(f.name)[<span style="color:#880000">1</span>])     <span style="color:#888888">#a.txt</span>
<span style="color:#880000">44</span> 
<span style="color:#880000">45</span> <span style="color:#888888">#closed用法,文件是否关闭</span>
<span style="color:#880000">46</span> print(f.closed)     <span style="color:#888888">#False</span>
<span style="color:#880000">47</span> f.close()
<span style="color:#880000">48</span> print(f.closed)     <span style="color:#888888">#True</span>
</span></span></span></span>
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> <span style="color:#888888"># coding:utf-8</span>
 <span style="color:#880000">2</span> <span style="color:#333333"><strong>import</strong></span> os
 <span style="color:#880000">3</span> 
 <span style="color:#880000">4</span> <span style="color:#888888">#文件读写简单封装:</span>
 <span style="color:#880000">5</span> <span style="color:#333333"><strong>class</strong></span> <span style="color:#880000"><strong>Open</strong></span>(object):
 <span style="color:#880000">6</span>     <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>__init__</strong></span>(self,path,mode=<span style="color:#880000">'w'</span>,is_return=True):
 <span style="color:#880000">7</span>         self.path=path
 <span style="color:#880000">8</span>         self.mode=mode
 <span style="color:#880000">9</span>         self.is_return=is_return
<span style="color:#880000">10</span> 
<span style="color:#880000">11</span>     <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>write</strong></span>(self,message):
<span style="color:#880000">12</span>         f=open(self.path,mode=self.mode)
<span style="color:#880000">13</span>         <span style="color:#333333"><strong>if</strong></span> message.endswith(<span style="color:#880000">"\n"</span>):
<span style="color:#880000">14</span>             self.is_return=<span style="color:#333333"><strong>False</strong></span>
<span style="color:#880000">15</span>             <span style="color:#333333"><strong>if</strong></span> self.is_return:
<span style="color:#880000">16</span>                 message=<span style="color:#880000">'%s\n'</span>%message
<span style="color:#880000">17</span>         <span style="color:#333333"><strong>try</strong></span>:
<span style="color:#880000">18</span>             f.write(message)
<span style="color:#880000">19</span>         <span style="color:#333333"><strong>finally</strong></span>:
<span style="color:#880000">20</span>             f.close()
<span style="color:#880000">21</span> 
<span style="color:#880000">22</span>     <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>read</strong></span>(self,is_strip=True):
<span style="color:#880000">23</span>         result=[]
<span style="color:#880000">24</span>         <span style="color:#333333"><strong>with</strong></span> open(self.path,mode=self.mode) <span style="color:#333333"><strong>as</strong></span> f:
<span style="color:#880000">25</span>             data=f.readlines()
<span style="color:#880000">26</span>         <span style="color:#333333"><strong>for</strong></span> line <span style="color:#333333"><strong>in</strong></span> data:
<span style="color:#880000">27</span>             <span style="color:#333333"><strong>if</strong></span> is_strip:   <span style="color:#888888">#is_strip需不需要去掉每个元素的\n</span>
<span style="color:#880000">28</span>                 temp=line.strip()
<span style="color:#880000">29</span>                 <span style="color:#333333"><strong>if</strong></span> temp != <span style="color:#880000">''</span>:
<span style="color:#880000">30</span>                     result.append(temp)
<span style="color:#880000">31</span>             <span style="color:#333333"><strong>else</strong></span>:
<span style="color:#880000">32</span>                 <span style="color:#333333"><strong>if</strong></span> line !=<span style="color:#880000">''</span>:
<span style="color:#880000">33</span>                     result.append(line)
<span style="color:#880000">34</span>         <span style="color:#333333"><strong>return</strong></span> result
<span style="color:#880000">35</span> 
<span style="color:#880000">36</span> <span style="color:#333333"><strong>if</strong></span> __name__==<span style="color:#880000">'__main__'</span>:
<span style="color:#880000">37</span>     carrent_path=os.getcwd()
<span style="color:#880000">38</span>     file_path=os.path.join(carrent_path,<span style="color:#880000">'a.txt'</span>)
<span style="color:#880000">39</span>     o=Open(file_path,mode=<span style="color:#880000">'r'</span>)
<span style="color:#880000">40</span>     data=o.read()
<span style="color:#880000">41</span>     print(data)
</span></span></span></span>

2.文件的应用

2.1.序列化

  • 初识序列化与反序列化
    • 序列化:将对象信息或数据结构信息进行转换,达到存储和传输的目的,最终的数据类型是有一定规则的字符串,可用于文件存储或网络传输
    • 反序列化:通过序列化生成的字符串反转回原来的形式
  • 可序列化的数据类型
    • 可序列化:number,str,list,tuple,dict(最常用的序列化数据类型)
    • 不可序列化:类,实例化对象,函数,集合不能进行序列化
  • Python中的json:通用序列化模块,所有编程语言都有,序列化与反序列化规则统一

  • Python中的pyckle:只使用python编程语言,pickle性能比json好

<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> # coding:utf-<span style="color:#880000">8</span>
 <span style="color:#880000">2</span> <span style="color:#333333"><strong>import</strong></span> json
 <span style="color:#880000">3</span> 
 <span style="color:#880000">4</span> a=<span style="color:#880000">99</span>
 <span style="color:#880000">5</span> b=<span style="color:#880000">'str'</span>
 <span style="color:#880000">6</span> c=(<span style="color:#880000">1</span>,<span style="color:#880000">2</span>,<span style="color:#880000">3</span>)
 <span style="color:#880000">7</span> d=[<span style="color:#880000">4</span>,<span style="color:#880000">5</span>,<span style="color:#880000">6</span>]
 <span style="color:#880000">8</span> e={<span style="color:#880000">"name"</span>:<span style="color:#880000">"中国"</span>}
 <span style="color:#880000">9</span> 
<span style="color:#880000">10</span> #json序列化
<span style="color:#880000">11</span> a_json=json.dumps(a)
<span style="color:#880000">12</span> <span style="color:#333333"><strong>print</strong></span>(a_json,type(a_json))   #<span style="color:#880000">99</span> <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'str'</span>>
<span style="color:#880000">13</span> b_json=json.dumps(b)
<span style="color:#880000">14</span> <span style="color:#333333"><strong>print</strong></span>(b_json,type(b_json))   #<span style="color:#880000">"str"</span> <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'str'</span>>
<span style="color:#880000">15</span> c_json=json.dumps(c)
<span style="color:#880000">16</span> <span style="color:#333333"><strong>print</strong></span>(c_json,type(c_json))   #[<span style="color:#880000">1</span>, <span style="color:#880000">2</span>, <span style="color:#880000">3</span>] <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'str'</span>>
<span style="color:#880000">17</span> d_json=json.dumps(d)
<span style="color:#880000">18</span> <span style="color:#333333"><strong>print</strong></span>(d_json,type(d_json))   #[<span style="color:#880000">4</span>, <span style="color:#880000">5</span>, <span style="color:#880000">6</span>] <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'str'</span>>
<span style="color:#880000">19</span> e_json=json.dumps(e)
<span style="color:#880000">20</span> <span style="color:#333333"><strong>print</strong></span>(e_json,type(e_json))   #{<span style="color:#880000">"name"</span>: <span style="color:#880000">"\u4e2d\u56fd"</span>} <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'str'</span>>
<span style="color:#880000">21</span> 
<span style="color:#880000">22</span> #json反序列化
<span style="color:#880000">23</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(a_json),type(json.loads(a_json)))      #<span style="color:#880000">99</span> <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'int'</span>>
<span style="color:#880000">24</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(b_json),type(json.loads(b_json)))      #str <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'str'</span>>
<span style="color:#880000">25</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(c_json),type(json.loads(c_json)))      #元组反序列化后数据类型变成了列表,[<span style="color:#880000">1</span>, <span style="color:#880000">2</span>, <span style="color:#880000">3</span>] <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'list'</span>>
<span style="color:#880000">26</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(d_json),type(json.loads(d_json)))      #[<span style="color:#880000">4</span>, <span style="color:#880000">5</span>, <span style="color:#880000">6</span>] <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'list'</span>>
<span style="color:#880000">27</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(e_json),type(json.loads(e_json)))      #{<span style="color:#880000">'name'</span>: <span style="color:#880000">'中国'</span>} <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'dict'</span>>
<span style="color:#880000">28</span> 
<span style="color:#880000">29</span> #特殊数据类型进行json序列化与反序列化
<span style="color:#880000">30</span> #布尔类型
<span style="color:#880000">31</span> <span style="color:#333333"><strong>print</strong></span>(json.dumps(<span style="color:#333333"><strong>True</strong></span>))                     #<span style="color:#333333"><strong>true</strong></span>
<span style="color:#880000">32</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(json.dumps(<span style="color:#333333"><strong>True</strong></span>)))         #<span style="color:#333333"><strong>True</strong></span>
<span style="color:#880000">33</span> <span style="color:#333333"><strong>print</strong></span>(json.dumps(<span style="color:#333333"><strong>False</strong></span>))                    #<span style="color:#333333"><strong>false</strong></span>
<span style="color:#880000">34</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(json.dumps(<span style="color:#333333"><strong>False</strong></span>)))        #<span style="color:#333333"><strong>False</strong></span>
<span style="color:#880000">35</span> #空类型
<span style="color:#880000">36</span> <span style="color:#333333"><strong>print</strong></span>(json.dumps(None))                     #<span style="color:#333333"><strong>null</strong></span>
<span style="color:#880000">37</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(json.dumps(None)))         #None
</span></span></span></span>
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> # coding:utf<span style="color:#880000">-8</span>
 <span style="color:#880000">2</span> <span style="color:#333333"><strong>import</strong></span> pickle
 <span style="color:#880000">3</span> 
 <span style="color:#880000">4</span> #pickle用法同json,此处不再过多演示
 <span style="color:#880000">5</span> e={<span style="color:#880000">"name"</span>:<span style="color:#880000">"中国"</span>}
 <span style="color:#880000">6</span> 
 <span style="color:#880000">7</span> #pickle序列化
 <span style="color:#880000">8</span> e_pickle=pickle.dumps(e)
 <span style="color:#880000">9</span> <span style="color:#397300">print</span>(e_pickle,<span style="color:#333333"><strong>type</strong></span>(e_pickle))  #b<span style="color:#880000">'\x80\x03}q\x00X\x04\x00\x00\x00nameq\x01X\x06\x00\x00\x00\xe4\xb8\xad\xe5\x9b\xbdq\x02s.'</span> <class <span style="color:#880000">'bytes'</span>>
<span style="color:#880000">10</span> 
<span style="color:#880000">11</span> #pickle反序列化
<span style="color:#880000">12</span> <span style="color:#397300">print</span>(pickle.loads(e_pickle),<span style="color:#333333"><strong>type</strong></span>(pickle.loads(e_pickle)))  #{<span style="color:#880000">'name'</span>: <span style="color:#880000">'中国'</span>} <class <span style="color:#880000">'dict'</span>>
</span></span></span></span>
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> <span style="color:#888888"># coding:utf-8</span>
 <span style="color:#880000">2</span> 
 <span style="color:#880000">3</span> <span style="color:#333333"><strong>import</strong></span> json
 <span style="color:#880000">4</span> 
 <span style="color:#880000">5</span> <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>read</strong></span>(path):
 <span style="color:#880000">6</span>     <span style="color:#333333"><strong>with</strong></span> open(<span style="color:#880000">'text.json'</span>,<span style="color:#880000">'r'</span>) <span style="color:#333333"><strong>as</strong></span> f:
 <span style="color:#880000">7</span>         data=f.read()
 <span style="color:#880000">8</span>     <span style="color:#333333"><strong>return</strong></span> json.loads(data)
 <span style="color:#880000">9</span> 
<span style="color:#880000">10</span> <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>write</strong></span>(path,data):
<span style="color:#880000">11</span>     <span style="color:#333333"><strong>with</strong></span> open(path,<span style="color:#880000">'w'</span>) <span style="color:#333333"><strong>as</strong></span> f:
<span style="color:#880000">12</span>         <span style="color:#333333"><strong>if</strong></span> isinstance(data,dict):
<span style="color:#880000">13</span>             _data=json.dumps(data)
<span style="color:#880000">14</span>             f.write(_data)
<span style="color:#880000">15</span>         <span style="color:#333333"><strong>else</strong></span>:
<span style="color:#880000">16</span>             <span style="color:#333333"><strong>raise</strong></span> TypeError(<span style="color:#880000">'data is dict'</span>)
<span style="color:#880000">17</span>     <span style="color:#333333"><strong>return</strong></span> <span style="color:#333333"><strong>True</strong></span>
<span style="color:#880000">18</span> 
<span style="color:#880000">19</span> data={<span style="color:#880000">"name"</span>:<span style="color:#880000">"张三"</span>,<span style="color:#880000">"age"</span>:<span style="color:#880000">18</span>}
<span style="color:#880000">20</span> 
<span style="color:#880000">21</span> <span style="color:#333333"><strong>if</strong></span> __name__==<span style="color:#880000">"__main__"</span>:
<span style="color:#880000">22</span>     write(<span style="color:#880000">'text.json'</span>,data)
<span style="color:#880000">23</span>     print(read(<span style="color:#880000">'text.json'</span>))
</span></span></span></span>

2.2.yaml的用法

2.2.1.yaml格式的介绍

2.2.2.Python的第三方包---pyyaml

2.2.3.读取yaml文件的方法

<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> #yaml文件内容
 <span style="color:#880000">2</span> ur<span style="color:#bc6060">l:</span>
 <span style="color:#880000">3</span>   http<span style="color:#bc6060">s:</span>//www.baidu.<span style="color:#333333"><strong>com</strong></span>
 <span style="color:#880000">4</span> <span style="color:#397300">type</span><span style="color:#bc6060">s:</span>
 <span style="color:#880000">5</span>   - 网页
 <span style="color:#880000">6</span>   - 百度百科
 <span style="color:#880000">7</span>   - 图片
 <span style="color:#880000">8</span> dict_1:
 <span style="color:#880000">9</span>   name:<span style="color:#880000">12</span>
<span style="color:#880000">10</span>   age:<span style="color:#880000">18</span>
</span></span></span></span>
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> <span style="color:#888888"># coding:utf-8</span>
 <span style="color:#880000">2</span> 
 <span style="color:#880000">3</span> <span style="color:#333333"><strong>import</strong></span> yaml
 <span style="color:#880000">4</span> <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>read</strong></span>(path):
 <span style="color:#880000">5</span>     <span style="color:#333333"><strong>with</strong></span> open(path,<span style="color:#880000">'r'</span>,encoding=<span style="color:#880000">"utf-8"</span>) <span style="color:#333333"><strong>as</strong></span> f:
 <span style="color:#880000">6</span>         data=f.read()
 <span style="color:#880000">7</span>     result=yaml.load(data,Loader=yaml.FullLoader)
 <span style="color:#880000">8</span>     <span style="color:#333333"><strong>return</strong></span> result
 <span style="color:#880000">9</span> 
<span style="color:#880000">10</span> <span style="color:#333333"><strong>if</strong></span> __name__==<span style="color:#880000">'__main__'</span>:
<span style="color:#880000">11</span>     print(read(<span style="color:#880000">"text.yaml"</span>))
</span></span></span></span>

回顾:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值