Python實(shí)現(xiàn)按特定格式對文件進(jìn)行讀寫的方法示例
本文實(shí)例講述了Python實(shí)現(xiàn)按特定格式對文件進(jìn)行讀寫的方法。分享給大家供大家參考,具體如下:
#! /usr/bin/env python
#coding=utf-8
class ResultFile(object):
def __init__(self, res):
self.res = res
def WriteFile(self):
fp = open('pre_result.txt', 'w')
print 'write start!'
try:
for item in self.res:
fp.write(item['host'])
fp.write('\r')
fp.write(str(item['cpu']))#write方法的實(shí)參需要為string類型
fp.write('\r')
fp.write(str(item['mem']))
fp.write('\n')
finally:
fp.close()
print 'write finish!'
def ReadFile(self):
res = []
fp = open('pre_result.txt', 'r')
try:
lines = fp.readlines()#讀取出全部數(shù)據(jù),按行存儲
finally:
fp.close()
for line in lines:
dict = {}
#print line.split() #like['compute21', '2', '4']
line_list = line.split() #默認(rèn)以空格為分隔符對字符串進(jìn)行切片
dict['host'] = line_list[0]
dict['cpu'] = int(line_list[1])#讀取出來的是字符
dict['mem'] = int(line_list[2])
res.append(dict)
return res
if __name__ == '__main__':
result_list=[{'host':'compute21', 'cpu':2, 'mem':4},{'host':'compute21', 'cpu':2, 'mem':4},
{'host':'compute22', 'cpu':2, 'mem':4},{'host':'compute23', 'cpu':2, 'mem':4},
{'host':'compute22', 'cpu':2, 'mem':4},{'host':'compute23', 'cpu':2, 'mem':4},
{'host':'compute24', 'cpu':2, 'mem':4}]
file_handle = ResultFile(result_list)
#1、寫入數(shù)據(jù)
#print 'write start!'
file_handle.WriteFile()
#print 'write finish!'
#2、讀取數(shù)據(jù)
res = file_handle.ReadFile()
print res
寫入的文件:

每一行的數(shù)據(jù)之間其實(shí)已經(jīng)加入空格。
運(yùn)行結(jié)果:
write start!
write finish!
[{'mem': 4, 'host': 'compute21', 'cpu': 2}, {'mem': 4, 'host':
'compute21', 'cpu': 2}, {'mem': 4, 'host': 'compute22', 'cpu': 2},
{'mem': 4, 'host': 'compute23', 'cpu': 2}, {'mem': 4, 'host':
'compute22', 'cpu': 2}, {'mem': 4, 'host': 'compute23', 'cpu': 2},
{'mem': 4, 'host': 'compute24', 'cpu': 2}]
實(shí)現(xiàn)了按原有格式寫入和讀取。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python編碼操作技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
使用python爬取taptap網(wǎng)站游戲截圖的步驟
這篇文章主要介紹了使用python爬取taptap游戲截圖的步驟,幫助大家更好的理解和學(xué)習(xí)使用python進(jìn)行爬蟲,感興趣的朋友可以了解下2021-05-05
Python的“二維”字典 (two-dimension dictionary)定義與實(shí)現(xiàn)方法
這篇文章主要介紹了Python的“二維”字典 (two-dimension dictionary)定義與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Python模擬實(shí)現(xiàn)類似二維數(shù)組形式的二維字典功能,需要的朋友可以參考下2016-04-04
python Django批量導(dǎo)入數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了python Django批量導(dǎo)入數(shù)據(jù)的相關(guān)資料感興趣的小伙伴們可以參考一下2016-03-03
Python實(shí)現(xiàn)文件/文件夾復(fù)制功能
在數(shù)據(jù)處理和文件管理的日常工作中,我們經(jīng)常需要復(fù)制文件夾及其子文件夾下的特定文件,手動操作不僅效率低下,而且容易出錯,因此,使用編程語言自動化這一任務(wù)顯得尤為重要,所以本文給大家介紹了使用Python實(shí)現(xiàn)文件/文件夾復(fù)制功能,需要的朋友可以參考下2025-04-04
python實(shí)現(xiàn)一次性封裝多條sql語句(begin end)
這篇文章主要介紹了python實(shí)現(xiàn)一次性封裝多條sql語句(begin end),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
對Python中range()函數(shù)和list的比較
下面小編就為大家分享一篇對Python中range()函數(shù)和list的比較,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04

