Python string 模块,splitfields() 实例源码
我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用string.splitfields()。
def sendportcmd(s, f, port):
hostname = gethostname()
hostaddr = gethostbyname(hostname)
hbytes = string.splitfields(hostaddr, '.')
pbytes = [repr(port//256), repr(port%256)]
bytes = hbytes + pbytes
cmd = 'PORT ' + string.joinfields(bytes, ',')
s.send(cmd + '\r\n')
code = getreply(f)
# Process an ftp reply and return the 3-digit reply code (as a string).
# The reply should be a line of text starting with a 3-digit number.
# If the 4th char is '-',it is a multi-line reply and is
# terminate by a line starting with the same 3-digit number.
# Any text while receiving the reply is echoed to the file.
#
def unctime(date):
if date == "None": return None
if not unctime_monthmap:
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
i = 0
for m in months:
i = i+1
unctime_monthmap[m] = i
words = string.split(date) # Day Mon DD HH:MM:SS YEAR
year = string.atoi(words[4])
month = unctime_monthmap[words[1]]
day = string.atoi(words[2])
[hh, mm, ss] = map(string.atoi, string.splitfields(words[3], ':'))
ss = ss - time.timezone
return time.mktime((year, month, day, hh, ss, 0, 0))
def Walk( root, recurse=0, pattern='*', return_folders=0 ):
import fnmatch, os, string
# initialize
result = []
# must have at least root folder
try:
names = os.listdir(root)
except os.error:
return result
# expand pattern
pattern = pattern or '*'
pat_list = string.splitfields( pattern , ';' )
# check each file
for name in names:
fullname = os.path.normpath(os.path.join(root, name))
# grab if it matches our pattern and entry type
for pat in pat_list:
if fnmatch.fnmatch(name, pat):
if os.path.isfile(fullname) or (return_folders and os.path.isdir(fullname)):
result.append(fullname)
continue
# recursively scan other folders,appending results
if recurse:
if os.path.isdir(fullname) and not os.path.islink(fullname):
result = result + Walk( fullname, recurse, pattern, return_folders )
return result
def getline(text):
for line in string.splitfields(text, '\n'):
co.tran(codisassembler, line)
def get_menu(selector, host, port):
f = send_request(selector, port)
list = []
while 1:
line = f.readline()
if not line:
print '(Unexpected EOF from server)'
break
if line[-2:] == CRLF:
line = line[:-2]
elif line[-1:] in CRLF:
line = line[:-1]
if line == '.':
break
if not line:
print '(Empty line from server)'
continue
typechar = line[0]
parts = string.splitfields(line[1:], TAB)
if len(parts) < 4:
print '(Bad line from server: %r)' % (line,)
continue
if len(parts) > 4:
print '(Extra info from server: %r)' % (parts[4:],)
parts.insert(0, typechar)
list.append(parts)
f.close()
return list
# Get a text file as a list of lines,with trailing CRLF stripped
def do_update(self):
name, option, column = self.format_list[self.format.get()]
s = commands.getoutput('ps -w ' + option)
list = splitfields(s, '\n')
self.header.set(list[0])
del list[0]
y = self.frame.vscroll.get()[0]
self.frame.list.delete(0, AtEnd())
for line in list:
self.frame.list.insert(0, line)
self.frame.list.yview(int(y))
def write(self, '\r\n')
self.data = self.data + data
self.check_data()
def addname(self, name):
# Domain name packing (section 4.1.4)
# Add a domain name to the buffer,possibly using pointers.
# The case of the first occurrence of a name is preserved.
# Redundant dots are ignored.
list = []
for label in string.splitfields(name, '.'):
if label:
if len(label) > 63:
raise Packerror('label too long')
list.append(label)
keys = []
for i in range(len(list)):
key = string.upper(string.joinfields(list[i:], '.'))
keys.append(key)
if key in self.index:
pointer = self.index[key]
break
else:
i = len(list)
pointer = None
# Do it into temporaries first so exceptions don't
# mess up self.index and self.buf
buf = ''
offset = len(self.buf)
index = []
for j in range(i):
label = list[j]
n = len(label)
if offset + len(buf) < 0x3FFF:
index.append((keys[j], offset + len(buf)))
else:
print 'DNS.Lib.Packer.addname:',
print 'warning: pointer too big'
buf = buf + (chr(n) + label)
if pointer:
buf = buf + pack16bit(pointer | 0xC000)
else:
buf = buf + '\0'
self.buf = self.buf + buf
for key, value in index:
self.index[key] = value
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。