日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

在python中修改.properties文件的操作

瀏覽:16日期:2022-07-30 17:45:15

在java 編程中,很多配置文件用鍵值對的方式存儲在 properties 文件中,可以讀取,修改。而且在java 中有 java.util.Properties 這個類,可以很方便的處理properties 文件, 在python 中雖然也有讀取配置文件的類ConfigParser, 但如果習慣java 編程的人估計更喜歡下面這個用python 實現的讀取 properties 文件的類:

'''A Python replacement for java.util.Properties classThis is modelled as closely as possible to the Java original. '''import sys,osimport reimport timeclass IllegalArgumentException(Exception): def __init__(self, lineno, msg): self.lineno = lineno self.msg = msg def __str__(self): s=’Exception at line number %d => %s’ % (self.lineno, self.msg) return sclass Properties(object): ''' A Python replacement for java.util.Properties ''' def __init__(self, props=None): # Note: We don’t take a default properties object # as argument yet # Dictionary of properties. self._props = {} # Dictionary of properties with ’pristine’ keys # This is used for dumping the properties to a file # using the ’store’ method self._origprops = {} # Dictionary mapping keys from property # dictionary to pristine dictionary self._keymap = {} self.othercharre = re.compile(r’(?<!)(s*=)|(?<!)(s*:)’) self.othercharre2 = re.compile(r’(s*=)|(s*:)’) self.bspacere = re.compile(r’(?!s$)’) def __str__(self): s=’{’ for key,value in self._props.items(): s = ’’.join((s,key,’=’,value,’, ’)) s=’’.join((s[:-2],’}’)) return s def __parse(self, lines): ''' Parse a list of lines and create an internal property dictionary ''' # Every line in the file must consist of either a comment # or a key-value pair. A key-value pair is a line consisting # of a key which is a combination of non-white space characters # The separator character between key-value pairs is a ’=’, # ’:’ or a whitespace character not including the newline. # If the ’=’ or ’:’ characters are found, in the line, even # keys containing whitespace chars are allowed. # A line with only a key according to the rules above is also # fine. In such case, the value is considered as the empty string. # In order to include characters ’=’ or ’:’ in a key or value, # they have to be properly escaped using the backslash character. # Some examples of valid key-value pairs: # # key value # key=value # key:value # key value1,value2,value3 # key value1,value2,value3 # value4, value5 # key # This key= this value # key = value1 value2 value3 # Any line that starts with a ’#’ is considerered a comment # and skipped. Also any trailing or preceding whitespaces # are removed from the key/value. # This is a line parser. It parses the # contents like by line. lineno=0 i = iter(lines) for line in i: lineno += 1 line = line.strip() # Skip null lines if not line: continue # Skip lines which are comments if line[0] == ’#’: continue # Some flags escaped=False # Position of first separation char sepidx = -1 # A flag for performing wspace re check flag = 0 # Check for valid space separation # First obtain the max index to which we # can search. m = self.othercharre.search(line) if m:first, last = m.span()start, end = 0, firstflag = 1wspacere = re.compile(r’(?<![=:])(s)’) else:if self.othercharre2.search(line): # Check if either ’=’ or ’:’ is present # in the line. If they are then it means # they are preceded by a backslash. # This means, we need to modify the # wspacere a bit, not to look for # : or = characters. wspacere = re.compile(r’(?<![])(s)’) start, end = 0, len(line) m2 = wspacere.search(line, start, end) if m2:# print ’Space match=>’,line# Means we need to split by space.first, last = m2.span()sepidx = first elif m:# print ’Other match=>’,line# No matching wspace char found, need# to split by either ’=’ or ’:’first, last = m.span()sepidx = last - 1# print line[sepidx] # If the last character is a backslash # it has to be preceded by a space in which # case the next line is read as part of the # same property while line[-1] == ’’:# Read next linenextline = i.next()nextline = nextline.strip()lineno += 1# This line will become part of the valueline = line[:-1] + nextline # Now split to key,value according to separation char if sepidx != -1:key, value = line[:sepidx], line[sepidx+1:] else:key,value = line,’’ self.processPair(key, value) def processPair(self, key, value): ''' Process a (key, value) pair ''' oldkey = key oldvalue = value # Create key intelligently keyparts = self.bspacere.split(key) # print keyparts strippable = False lastpart = keyparts[-1] if lastpart.find(’ ’) != -1: keyparts[-1] = lastpart.replace(’’,’’) # If no backspace is found at the end, but empty # space is found, strip it elif lastpart and lastpart[-1] == ’ ’: strippable = True key = ’’.join(keyparts) if strippable: key = key.strip() oldkey = oldkey.strip() oldvalue = self.unescape(oldvalue) value = self.unescape(value) self._props[key] = value.strip() # Check if an entry exists in pristine keys if self._keymap.has_key(key): oldkey = self._keymap.get(key) self._origprops[oldkey] = oldvalue.strip() else: self._origprops[oldkey] = oldvalue.strip() # Store entry in keymap self._keymap[key] = oldkey def escape(self, value): # Java escapes the ’=’ and ’:’ in the value # string with backslashes in the store method. # So let us do the same. newvalue = value.replace(’:’,’:’) newvalue = newvalue.replace(’=’,’=’) return newvalue def unescape(self, value): # Reverse of escape newvalue = value.replace(’:’,’:’) newvalue = newvalue.replace(’=’,’=’) return newvalue def load(self, stream): ''' Load properties from an open file stream ''' # For the time being only accept file input streams if type(stream) is not file: raise TypeError,’Argument should be a file object!’ # Check for the opened mode if stream.mode != ’r’: raise ValueError,’Stream should be opened in read-only mode!’ try: lines = stream.readlines() self.__parse(lines) except IOError, e: raise def getProperty(self, key): ''' Return a property for the given key ''' return self._props.get(key,’’) def setProperty(self, key, value): ''' Set the property for the given key ''' if type(key) is str and type(value) is str: self.processPair(key, value) else: raise TypeError,’both key and value should be strings!’ def propertyNames(self): ''' Return an iterator over all the keys of the property dictionary, i.e the names of the properties ''' return self._props.keys() def list(self, out=sys.stdout): ''' Prints a listing of the properties to the stream ’out’ which defaults to the standard output ''' out.write(’-- listing properties --n’) for key,value in self._props.items(): out.write(’’.join((key,’=’,value,’n’))) def store(self, out, header=''): ''' Write the properties list to the stream ’out’ along with the optional ’header’ ''' if out.mode[0] != ’w’: raise ValueError,’Steam should be opened in write mode!’ try: out.write(’’.join((’#’,header,’n’))) # Write timestamp tstamp = time.strftime(’%a %b %d %H:%M:%S %Z %Y’, time.localtime()) out.write(’’.join((’#’,tstamp,’n’))) # Write properties from the pristine dictionary for prop, val in self._origprops.items():out.write(’’.join((prop,’=’,self.escape(val),’n’))) out.close() except IOError, e: raise def getPropertyDict(self): return self._props def __getitem__(self, name): ''' To support direct dictionary like access ''' return self.getProperty(name) def __setitem__(self, name, value): ''' To support direct dictionary like access ''' self.setProperty(name, value) def __getattr__(self, name): ''' For attributes not found in self, redirect to the properties dictionary ''' try: return self.__dict__[name] except KeyError: if hasattr(self._props,name):return getattr(self._props, name)if __name__=='__main__': p = Properties() p.load(open(’test2.properties’)) p.list() print p print p.items() print p[’name3’] p[’name3’] = ’changed = value’ print p[’name3’] p[’new key’] = ’new value’ p.store(open(’test2.properties’,’w’))

當然,測試這個類你需要在程序目錄下簡歷test2.properties 文件。才可以看到效果,基本可以達到用python 讀寫 properties 文件的效果.

補充知識:python修改配置文件某個字段

思路:要修改的文件filepath

在python中修改.properties文件的操作

將修改后的文件寫入f2,刪除filepath,將f2名字改為filepath,從而達到修改

修改的字段可以參數化,即下面出現的 lilei 可以參數化

imort ostag=“jdbc.cubedata.username=”midifyInfo=“jdbc.cubedata.username=lilei”f1=filepathf2=application.applicationfileInfo=open(filepath)

在python中修改.properties文件的操作

以上這篇在python中修改.properties文件的操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产成人精品一区二区三区免费 | 久久亚洲精品中文字幕| 日韩精品午夜视频| 亚洲精品乱码| 久久激情综合网| 久久xxx视频| 日韩a一区二区| 久久人人精品| 伊人久久亚洲影院| 亚洲精品小说| 免费久久精品视频| 日韩不卡一区二区三区| 国产精品网站在线看| 激情中国色综合| 久久激情网站| 视频在线在亚洲| 久久国产三级精品| 精品久久电影| 免费视频亚洲| 涩涩涩久久久成人精品| 国产精品毛片视频| 亚洲黄色免费看| 亚洲一区日本| 国产欧美在线| 91看片一区| 欧美91福利在线观看| 蜜臀av一区二区三区| 7777精品| 国产不卡av一区二区| 欧美一区二区性| 亚洲精品在线二区| 美女毛片一区二区三区四区最新中文字幕亚洲 | 成人羞羞视频在线看网址| 欧美在线亚洲| 日韩和欧美的一区| 日韩av片子| 免费成人性网站| 免费看一区二区三区| 青青久久av| 日韩在线网址| 国产不卡人人| 美女被久久久| 麻豆久久一区二区| 亚洲二区三区不卡| 国产精品亚洲综合色区韩国| 久久美女精品| 国产日韩一区二区三区在线播放| 日韩在线不卡| 日韩美女国产精品| 视频小说一区二区| 国产欧美一区二区精品久久久 | 精品资源在线| 91久久国产| 国产日韩欧美三区| 欧美精品一线| 国产成人久久精品一区二区三区| 欧美专区一区二区三区| 久久精品国产99国产精品| 国产午夜久久| 国产成人精品一区二区免费看京| 蜜臀av国产精品久久久久| 欧美韩日一区| 婷婷久久免费视频| 欧美日韩一二| 麻豆视频观看网址久久| 视频一区免费在线观看| 国产 日韩 欧美 综合 一区| 免费在线视频一区| 午夜久久中文| 国产精品chinese| 亚洲人成精品久久久| 99成人超碰| 精品一区二区三区的国产在线观看| 久久99伊人| 欧美日韩尤物久久| 国产精品极品| 亚洲综合中文| 欧美日韩日本国产亚洲在线| 精品99在线| 久久精品72免费观看| 日韩制服丝袜av| 电影亚洲精品噜噜在线观看| 欧美色综合网| 视频一区二区国产| 99久久久久国产精品| 久久精品国产在热久久| 91免费精品国偷自产在线在线| 最新日韩av| 欧美日中文字幕| 福利在线免费视频| 欧美黄页在线免费观看| 日本va欧美va精品| 国产高清久久| 99成人超碰| 亚洲精品在线影院| 麻豆网站免费在线观看| 国产精品啊啊啊| 日本伊人久久| 亚洲精品在线a| 乱人伦精品视频在线观看| 亚洲午夜在线| 99热国内精品| 99国产精品免费视频观看| 成人三级高清视频在线看| 麻豆精品新av中文字幕| 国产精品日韩精品中文字幕| 日韩免费精品| 日本一区二区三区中文字幕| 中文字幕一区二区av| 蜜臀精品一区二区三区在线观看| 日韩视频久久| 国产精品免费看| 久久中文字幕av一区二区不卡| 日韩高清不卡| 久久精品中文| 亚洲福利免费| av一区二区高清| 午夜欧美视频| 午夜一级久久| 最新国产精品视频| 四虎精品一区二区免费| 涩涩涩久久久成人精品| 亚洲2区在线| 欧美一区二区三区久久| 久久精品99国产精品日本| 国产欧美日韩亚洲一区二区三区| 国产精品一区高清| 国产精品一区二区99| 麻豆精品蜜桃视频网站| 国产成人精品亚洲线观看| 国产资源在线观看入口av| 欧美精品高清| 欧美亚洲激情| 美日韩精品视频| 日韩中文字幕| 国产精品久久久久久妇女| 国产一区二区三区精品在线观看| 亚洲天堂资源| 亚洲激情精品| 日韩精品一区二区三区中文在线 | 在线一区免费观看| 美女久久一区| 欧美日韩在线精品一区二区三区激情综合| 日韩成人一级| 欧美aⅴ一区二区三区视频| 午夜影院一区| 国产色综合网| 日本一区免费网站| 美女性感视频久久| 国产一区二区三区不卡视频网站| 深夜福利视频一区二区| 国产模特精品视频久久久久| 日韩国产精品久久久久久亚洲| 欧美日韩亚洲一区二区三区在线 | 日韩在线视频精品| 欧美网站在线| 日韩av网站在线免费观看| 国语精品一区| 欧美日韩三区| 日韩va欧美va亚洲va久久| 国模大尺度视频一区二区| 婷婷国产精品| 在线国产日韩| 精品久久久中文字幕| 婷婷色综合网| 欧美亚洲色图校园春色| 精品日韩视频| 日韩有吗在线观看| 91视频一区| 欧美中文日韩| 美女久久久久久 | 久久电影tv| 手机精品视频在线观看| 麻豆国产精品777777在线| 亚洲最新无码中文字幕久久| 日韩影院免费视频| 国产 日韩 欧美 综合 一区 | 99国产精品99久久久久久粉嫩| 欧美久久香蕉| 久久久五月天| 欧美日韩亚洲一区| 五月天综合网站| 国产精品国产三级在线观看| 午夜久久美女| 久久伊人久久| 亚洲一区欧美激情| 精品精品国产三级a∨在线| 久久午夜精品| 精品视频久久| 亚州精品视频| 久久影视一区| 久久不见久久见中文字幕免费| 一区二区亚洲精品| 精品美女视频| 日韩在线观看中文字幕| 日韩精品看片| 老鸭窝一区二区久久精品| 蜜臀久久99精品久久久画质超高清 | 久久一区二区三区喷水| 奇米亚洲欧美|