Python2.7 中的字符串格式以及 Net 命令的结果
String Formatting in Python2.7 with result from Net commands
我创建了一个将用户名写入文件的批处理文件。它完美运行并清理 net user
并将用户名写入文件,因此它看起来像这样:
管理员 Michael Guest<br>
钢琴家比利乔治
我不知道会有多少个用户名,所以我的问题是:我不知道名字的长度,我怎样才能清理未确定的名字之间的白色 space将要处理,因此不知道会有多少 space。
我的 python 程序应该从文件中读取这些名称并将它们变成一个列表。我打算只使用 .split(" ") 所以理想情况下有人可以建议一种方法将每个名称之间的差异缩小到一个 space。我已经看过 .format 方法,它似乎无法胜任这项任务。如果有某种可读的方式(可疑)来批量格式化它,我也很开放。
顺便说一句:我考虑过简单地重定向 dir /B C:\Users
的输出,但这在这种情况下不起作用。
使用不带 sep
参数的 .split()
:
string.split(s[, sep[, maxsplit]])
Return a list of the words of the string s
. If the optional second
argument sep
is absent or None
, the words are separated by
arbitrary strings of whitespace characters (space, tab, newline,
return, formfeed). If the second argument sep
is present and not
None
, it specifies a string to be used as the word separator. The
returned list will then have one more item than the number of
non-overlapping occurrences of the separator in the string. If
maxsplit
is given, at most maxsplit
number of splits occur, and
the remainder of the string is returned as the final element of the
list (thus, the list will have at most maxsplit+1
elements). If
maxsplit
is not specified or -1
, then there is no limit on the
number of splits (all possible splits are made).
The behavior of split on an empty string depends on the value of
sep
. If sep
is not specified, or specified as None
, the result
will be an empty list. If sep
is specified as any string, the result
will be a list containing one element which is an empty string.
示例:
>>> x='Administrator CLIENT1 Guest'
>>> x.split(' ')
['Administrator', '', '', '', '', '', '', '', '', '', '', '', 'CLIENT1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '','Guest']
>>> x.split()
['Administrator', 'CLIENT1', 'Guest']
>>>
另一种方法:
>>> import string
>>> x='Administrator CLIENT1 Guest'
>>> string.split(x,' ')
['Administrator', '', '', '', '', '', '', '', '', '', '', '', 'CLIENT1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '','Guest']
>>> string.split(x)
['Administrator', 'CLIENT1', 'Guest']
>>>
我创建了一个将用户名写入文件的批处理文件。它完美运行并清理 net user
并将用户名写入文件,因此它看起来像这样:
管理员 Michael Guest<br>
钢琴家比利乔治
我不知道会有多少个用户名,所以我的问题是:我不知道名字的长度,我怎样才能清理未确定的名字之间的白色 space将要处理,因此不知道会有多少 space。
我的 python 程序应该从文件中读取这些名称并将它们变成一个列表。我打算只使用 .split(" ") 所以理想情况下有人可以建议一种方法将每个名称之间的差异缩小到一个 space。我已经看过 .format 方法,它似乎无法胜任这项任务。如果有某种可读的方式(可疑)来批量格式化它,我也很开放。
顺便说一句:我考虑过简单地重定向 dir /B C:\Users
的输出,但这在这种情况下不起作用。
使用不带 sep
参数的 .split()
:
string.split(s[, sep[, maxsplit]])
Return a list of the words of the string
s
. If the optional second argumentsep
is absent orNone
, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). If the second argumentsep
is present and notNone
, it specifies a string to be used as the word separator. The returned list will then have one more item than the number of non-overlapping occurrences of the separator in the string. Ifmaxsplit
is given, at mostmaxsplit
number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at mostmaxsplit+1
elements). Ifmaxsplit
is not specified or-1
, then there is no limit on the number of splits (all possible splits are made).The behavior of split on an empty string depends on the value of
sep
. Ifsep
is not specified, or specified asNone
, the result will be an empty list. Ifsep
is specified as any string, the result will be a list containing one element which is an empty string.
示例:
>>> x='Administrator CLIENT1 Guest'
>>> x.split(' ')
['Administrator', '', '', '', '', '', '', '', '', '', '', '', 'CLIENT1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '','Guest']
>>> x.split()
['Administrator', 'CLIENT1', 'Guest']
>>>
另一种方法:
>>> import string
>>> x='Administrator CLIENT1 Guest'
>>> string.split(x,' ')
['Administrator', '', '', '', '', '', '', '', '', '', '', '', 'CLIENT1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '','Guest']
>>> string.split(x)
['Administrator', 'CLIENT1', 'Guest']
>>>