python文件的读写操作 ╰+攻爆jí腚メ 2022-05-31 12:56 188阅读 0赞 ### python读文件操作 ### <table> <thead> <tr> <th>命令</th> <th>解释</th> </tr> </thead> <tbody> <tr> <td>read([size])</td> <td>读取文件(读取size字节,默认读取全部)</td> </tr> <tr> <td>readline([size])</td> <td>读取一行</td> </tr> <tr> <td>readline([size])</td> <td>读取缓冲buf(io.DEFAULT_SET_BUFFER),返回每一行所组成的列表</td> </tr> </tbody> </table> with open('text.txt') as f: lines = f.readlines() # 读取文本中所有内容,并保存在一个列表中,列表中每一个元素对应一行数据 print lines # 每一行数据都包含了换行符 for line in lines: print line.rstrip() #删除文末空白 读取数据时,希望将str直接从json文件中读取数据并转换成dict,可以使用dump.loads()或dump.load() import json test_filename = ('/home/test_json.json') jsObj = json.load(open(test_filename)) print(jsObj) print(type(jsObj)) for key in jsObj.keys(): print('key: %s value: %s' % (key,jsObj.get(key))) #输出 { u'a': u'1111', u'c': u'3333', u'b': u'2222', u'd': u'4444'} <type 'dict'> key: a value: 1111 key: c value: 3333 key: b value: 2222 key: d value: 4444 ### python写文件操作 ### <table> <thead> <tr> <th>命令</th> <th>解释</th> </tr> </thead> <tbody> <tr> <td>write(str)</td> <td>将字符串写入文件</td> </tr> <tr> <td>writelines(sequence_of_strings)</td> <td>写多行到文件,参数为可迭代的对象</td> </tr> </tbody> </table> filename = 'test.txt' with open(filename,'w') as f: # 如果filename不存在会自动创建, 'w'表示写数据,写之前会清空文件中的原有数据! f.write("I am a test.\n") 如果读写的文件中遇到不同类型的数据,此时可以使用pickle.dump来写入 import pickle data1 = { 'a': [1, 2.0, 3, 4+6j], 'b': ('string', u'Unicode string'), 'c': None} selfref_list = [1, 2, 3] selfref_list.append(selfref_list) output = open('data.pkl', 'wb') # Pickle dictionary using protocol 0. pickle.dump(data1, output) # Pickle the list using the highest protocol available. pickle.dump(selfref_list, output, -1) output.close() 另外,如果写入数据时数据格式是str字符时,可以使用json.dumps()或json.dump()函数将字典转化为str,然后继续写入。 举例说明: import json test = { 'a':'1111','b':'2222','c':'3333'} test_str = json.dumps(test) file_name = '/home/test.txt' with open(file_name,'w') as f: f.write(test_str) f.close() 更具体的说明可以见参考文献4. ### 3.几种不同的读写方式 ### <table> <thead> <tr> <th>符号</th> <th>解释</th> </tr> </thead> <tbody> <tr> <td>r</td> <td>只读</td> </tr> <tr> <td>w</td> <td>写之前会清空文件的内容</td> </tr> <tr> <td>a</td> <td>追加的方式,在原本内容中继续写</td> </tr> <tr> <td>r+</td> <td>可读可写</td> </tr> <tr> <td>w+</td> <td>打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。</td> </tr> <tr> <td>a+</td> <td>打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。</td> </tr> <tr> <td>rb wb ab</td> <td>读写二进制</td> </tr> </tbody> </table> 参考文献 1.[http://blog.csdn.net/sinat\_34474705/article/details/77389258][http_blog.csdn.net_sinat_34474705_article_details_77389258] 2.[https://www.cnblogs.com/emily-qin/p/7001053.html][https_www.cnblogs.com_emily-qin_p_7001053.html] 3.[https://www.cnblogs.com/onepiece-andy/p/python-wrfile.html][https_www.cnblogs.com_onepiece-andy_p_python-wrfile.html] 4. [http://blog.csdn.net/mr\_evanchen/article/details/77879967][http_blog.csdn.net_mr_evanchen_article_details_77879967] [http_blog.csdn.net_sinat_34474705_article_details_77389258]: http://blog.csdn.net/sinat_34474705/article/details/77389258 [https_www.cnblogs.com_emily-qin_p_7001053.html]: https://www.cnblogs.com/emily-qin/p/7001053.html [https_www.cnblogs.com_onepiece-andy_p_python-wrfile.html]: https://www.cnblogs.com/onepiece-andy/p/python-wrfile.html [http_blog.csdn.net_mr_evanchen_article_details_77879967]: http://blog.csdn.net/mr_evanchen/article/details/77879967
还没有评论,来说两句吧...