2024-05-23 17:13:33 发布
网友
如何用python(可能是re模块)解析这个字符串并用这个数据创建数组?你知道吗
map: mp_rust num score ping guid name lastmsg address qport rate --- ----- ---- -------------------------------- --------------- ------- --------------------- ----- -----
对于固定宽度格式,string slicing可能是解析的最佳方式:
num = s[0:3] score = s[4:9] ping = s[10:14] guid = s[15:47] name = s[48:63] ...
一定要strip去掉多余的空白,必要时转换成int。你知道吗
通过将结果存储在list中,可以创建结果的“数组”:
arr = [num, score, ping, guid, name, lastmsg, address, qport, rate]
除非您知道数据的确切长度(名称可以是“jack”或“theodore”,只是说说而已),否则在空格处拆分可能是一种更聪明的方法。这是一种细线切割,但不那么静态。你知道吗
>>> s = 'num score ping guid name lastmsg address' >>> num, score, ping, guid, name, lastmsg, address = s.split()
如果这些值可能包含空格,那么必须使用regex。你知道吗
对于固定宽度格式,string slicing可能是解析的最佳方式:
一定要strip去掉多余的空白,必要时转换成int。你知道吗
通过将结果存储在list中,可以创建结果的“数组”:
除非您知道数据的确切长度(名称可以是“jack”或“theodore”,只是说说而已),否则在空格处拆分可能是一种更聪明的方法。这是一种细线切割,但不那么静态。你知道吗
如果这些值可能包含空格,那么必须使用regex。你知道吗
相关问题 更多 >
编程相关推荐