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

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

在java 編程中,很多配置文件用鍵值對的方式存儲在 properties 文件中,可以讀取,修改。而且在java 中有 java.util.Properties 這個類,可以很方便的處理properties 文件, 在python 中雖然也有讀取配置文件的類ConfigParser, 但如果習慣java 編程的人估計更喜歡下面這個用python 實現(xiàn)的讀取 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,從而達到修改

修改的字段可以參數(shù)化,即下面出現(xiàn)的 lilei 可以參數(shù)化

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

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

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

標簽: Python 編程
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲高清二区| 国产毛片久久| 亚洲美女91| 日韩中文欧美在线| 免费人成在线不卡| 免费观看在线综合| 91国语精品自产拍| 伊人影院久久| 亚洲精品美女91| 日韩美女国产精品| 欧美亚洲福利| 国产亚洲激情| 日本va欧美va瓶| 深夜福利一区| 亚洲精品免费观看| 奇米色欧美一区二区三区| 久久亚洲视频| 丝瓜av网站精品一区二区 | 亚洲高清激情| 亚洲高清av| 天堂网在线观看国产精品| 91精品国产福利在线观看麻豆| av综合电影网站| 久久香蕉国产| 久久av在线| 亚洲三级国产| 国产精品一区二区av交换| 久久不见久久见中文字幕免费| 国产欧美日韩精品一区二区免费 | 国精品一区二区| 亚洲二区精品| 国产乱码精品一区二区亚洲| av日韩中文| 在线日韩欧美| 蜜臀va亚洲va欧美va天堂| 国产精品视频一区二区三区综合| 久久亚洲精品中文字幕| 中文字幕在线高清| 午夜在线精品偷拍| 日本中文字幕一区二区视频| 香蕉精品视频在线观看| 日本不卡高清视频| 欧美日韩一二| 欧美久久久网站| 亚洲一区二区毛片| 精品视频久久| 亚洲精品亚洲人成在线观看| 高清一区二区三区av| 美国三级日本三级久久99| 精品三级av| 奇米亚洲欧美| 91看片一区| 国产欧美亚洲精品a| 综合亚洲视频| 97se综合| 精品免费av| 日韩不卡在线观看日韩不卡视频| 欧美日一区二区| 日本不卡高清| 亚洲精华国产欧美| 欧美中文字幕一区二区| 精品国产网站| 欧美freesex黑人又粗又大| 亚洲免费影视| 亚洲一级特黄| 久久精品国产一区二区| 久久精品福利| 亚洲欧美日韩综合国产aⅴ| 在线综合亚洲| 国产精品美女| 免费黄色成人| 国产亚洲福利| 99国产精品久久久久久久成人热 | 人人精品人人爱| 91精品一区国产高清在线gif| 黄色网一区二区| 精品网站999| 亚洲国产成人二区| 成人亚洲一区二区| 99久久精品网站| 久久成人国产| 欧美亚洲网站| 天堂久久av| 日韩激情一区二区| 欧美一区不卡| 国产精品igao视频网网址不卡日韩| 99国产精品私拍| 在线亚洲自拍| 蜜桃久久久久久| 日本不卡一二三区黄网| 国产欧美自拍| 福利一区二区三区视频在线观看| 国产精品**亚洲精品| 福利一区二区| 国产主播一区| 免费看黄色91| 亚洲精品九九| 国产精品久久久久久久久免费高清| 日韩高清在线不卡| 国产亚洲高清在线观看| 久久不卡国产精品一区二区| 国产调教精品| 成人污污视频| 欧美日韩精品一本二本三本| 美女91精品| 国产精品久久亚洲不卡| 欧美不卡高清一区二区三区| 国产精品普通话对白| 奇米狠狠一区二区三区| 国产中文欧美日韩在线| 伊人久久亚洲影院| 97精品国产99久久久久久免费| 丰满少妇一区| 美国三级日本三级久久99 | 久久中文字幕导航| 日韩在线欧美| 日韩区一区二| 亚洲福利专区| 国产传媒av在线| 日韩中文字幕一区二区高清99| 国产不卡一区| 视频一区二区中文字幕| 欧美a一区二区| 视频一区二区中文字幕| 国产一区日韩一区| 国产 日韩 欧美 综合 一区| 蜜臀久久99精品久久久画质超高清| 国产一区2区| 国产欧美二区| 免费成人性网站| 欧美精品91| 午夜电影一区| 在线精品视频在线观看高清| 国产精品高颜值在线观看| 国产精品一区亚洲| 亚洲美女91| 伊人精品久久| 日韩一区精品字幕| 亚洲黄页一区| 日韩视频中文| 蜜桃视频欧美| 在线亚洲观看| 午夜宅男久久久| 亚洲欧美日韩综合国产aⅴ| 91久久视频| 欧美69视频| 午夜精品影院| 欧美日韩精品一区二区视频| 日本不卡免费高清视频在线| 日韩一区电影| 久久中文字幕一区二区| 国产日韩中文在线中文字幕| 欧美日韩日本国产亚洲在线| 日本不卡高清视频| 色偷偷色偷偷色偷偷在线视频| 88xx成人免费观看视频库| 日韩免费一区| 亚洲激情黄色| 青草综合视频| 精品72久久久久中文字幕| 麻豆极品一区二区三区| 九九九精品视频| 少妇精品导航| 欧美中文日韩| 国产精品多人| 伊人久久在线| 日韩中文字幕91| 国产欧美欧美| 国产精品不卡| 亚洲18在线| 中文字幕在线官网| 蜜桃久久精品一区二区| 国产精品magnet| 黑丝一区二区三区| 国产伦一区二区三区| 日韩综合在线| 亚洲欧美成人综合| 国产精品99精品一区二区三区∴| 99久精品视频在线观看视频| 免费国产亚洲视频| 四虎8848精品成人免费网站| 蜜臀久久99精品久久一区二区| 日本成人在线不卡视频| sm久久捆绑调教精品一区| 日韩综合一区二区| 综合日韩av| 国产精品美女久久久浪潮软件| 久久国产精品免费一区二区三区| 久久亚洲国产| 国产一区二区三区亚洲| 午夜久久福利| 久久中文在线| 综合激情一区| 欧美一区二区性| 久久精品一本| 国产日韩欧美三级| 亚洲一二av| 久久av一区二区三区| 久久三级视频| 国产在线看片免费视频在线观看|