Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何解析这个字符串_Python_Regex_Python 3.x - Fatal编程技术网

Python 如何解析这个字符串

Python 如何解析这个字符串,python,regex,python-3.x,Python,Regex,Python 3.x,如何用python(可能是re模块)解析这个字符串,并用这个数据创建数组 map: mp_rust num score ping guid name lastmsg address qport rate --- ----- ---- -------------------------------- --------------- ------- --------------------- -

如何用python(可能是re模块)解析这个字符串,并用这个数据创建数组

map: mp_rust
num score ping guid                             name            lastmsg address               qport rate
--- ----- ---- -------------------------------- --------------- ------- --------------------- ----- -----

使用固定宽度格式,可能是解析以下内容的最佳方式:

num = s[0:3]
score = s[4:9]
ping = s[10:14]
guid = s[15:47]
name = s[48:63]
 ...
确保去掉多余的空白,并在必要时转换为

您可以通过将结果存储在以下位置来创建结果的“数组”:


除非您知道数据的确切长度(名称可以是“jack”或“theodore”,仅此而已),否则在空格处拆分可能是一种更聪明的方法。这是一种细线切割,但不那么静态

>>> s = 'num score ping guid                 name            lastmsg address'
>>> num, score, ping, guid, name, lastmsg, address = s.split()

如果这些值可能包含空格,则必须使用正则表达式。

您希望得到什么输出?这看起来像一个标题,下面还有更多的行吗?不完整,请把它变成一个真正的问题------将是用户的数据,该句子不解析。除非“num”将始终是“num”,否则[0:3]不会剪切它。我会选择
s.split()[0]
等等。一些评论会有所帮助。也许我错得太大了,即使投反对票也不会让我意识到这个问题。希望看到更广阔的前景
>>> s = 'num score ping guid                 name            lastmsg address'
>>> num, score, ping, guid, name, lastmsg, address = s.split()