一、题目与解
把下列字符串转为列表格式输出
top_ide_trend = """
Rank Change IDE Share Trend
1 Visual Studio 29.24 % +3.5 %
2 Eclipse 13.91 % -2.9 %
3 Visual Studio Code 12.07 % +3.3 %
4 Android Studio 9.13 % -2.5 %
5 pyCharm 8.43 % +0.7 %
6 IntelliJ 6.7 % +0.8 %
7 NetBeans 4.82 % -0.3 %
8 Sublime Text 3.49 % -0.2 %
9 Xcode 3.37 % -1.2 %
10 Atom 3.25 % -0.5 %
11 Code::Blocks 2.16 % +0.2 %
12 Vim 0.79 % -0.1 %
13 Xamarin 0.48 % -0.1 %
14 PhpStorm 0.46 % -0.2 %
15 geany 0.39 % +0.2 %
16 Komodo 0.31 % -0.2 %
17 Qt Creator 0.26 % -0.0 %
18 Emacs 0.18 % -0.1 %
19 JDeveloper 0.13 % -0.0 %
20 RAD Studio 0.08 % -0.0 %
21 JCreator 0.07 % -0.0 %
22 Light Table 0.07 % -0.0 %
23 MonoDevelop 0.06 % -0.0 %
24 SharpDevelop 0.03 % -0.0 %
25 Eric Python 0.03 % -0.0 %
26 Aptana 0.02 % -0.0 %
27 RubyMine 0.02 % -0.0 %
28 Coda 2 0.02 % +0.0 %
29 Monkey Studio 0.01 % -0.0 %
30 DrJava 0.01 % -0.0 %
"""
输出如下:
[
{'Rank': '1', 'Change': '', 'IDE': 'Visual Studio', 'Share': '29.24 %', 'Trend': '+3.5 %'},
{'Rank': '2', 'Change': '', 'IDE': 'Eclipse', 'Share': '13.91 %', 'Trend': '-2.9 %'},
{'Rank': '3', 'Change': '', 'IDE': 'Visual Studio Code', 'Share': '12.07 %', 'Trend': '+3.3 %'},
{'Rank': '4', 'Change': '', 'IDE': 'Android Studio', 'Share': '9.13 %', 'Trend': '-2.5 %'},
{'Rank': '5', 'Change': '', 'IDE': 'pyCharm', 'Share': '8.43 %', 'Trend': '+0.7 %'},
{'Rank': '6', 'Change': '', 'IDE': 'IntelliJ', 'Share': '6.7 %', 'Trend': '+0.8 %'},
{'Rank': '7', 'Change': '', 'IDE': 'NetBeans', 'Share': '4.82 %', 'Trend': '-0.3 %'},
{'Rank': '8', 'Change': '', 'IDE': 'Sublime Text', 'Share': '3.49 %', 'Trend': '-0.2 %'},
{'Rank': '9', 'Change': '', 'IDE': 'Xcode', 'Share': '3.37 %', 'Trend': '-1.2 %'},
{'Rank': '10', 'Change': '', 'IDE': 'Atom', 'Share': '3.25 %', 'Trend': '-0.5 %'},
{'Rank': '11', 'Change': '', 'IDE': 'Code::Blocks', 'Share': '2.16 %', 'Trend': '+0.2 %'},
{'Rank': '12', 'Change': '', 'IDE': 'Vim', 'Share': '0.79 %', 'Trend': '-0.1 %'},
{'Rank': '13', 'Change': '', 'IDE': 'Xamarin', 'Share': '0.48 %', 'Trend': '-0.1 %'},
{'Rank': '14', 'Change': '', 'IDE': 'PhpStorm', 'Share': '0.46 %', 'Trend': '-0.2 %'},
{'Rank': '15', 'Change': '', 'IDE': 'geany', 'Share': '0.39 %', 'Trend': '+0.2 %'},
{'Rank': '16', 'Change': '', 'IDE': 'Komodo', 'Share': '0.31 %', 'Trend': '-0.2 %'},
{'Rank': '17', 'Change': '', 'IDE': 'Qt Creator', 'Share': '0.26 %', 'Trend': '-0.0 %'},
{'Rank': '18', 'Change': '', 'IDE': 'Emacs', 'Share': '0.18 %', 'Trend': '-0.1 %'},
{'Rank': '19', 'Change': '', 'IDE': 'JDeveloper', 'Share': '0.13 %', 'Trend': '-0.0 %'},
{'Rank': '20', 'Change': '', 'IDE': 'RAD Studio', 'Share': '0.08 %', 'Trend': '-0.0 %'},
{'Rank': '21', 'Change': '', 'IDE': 'JCreator', 'Share': '0.07 %', 'Trend': '-0.0 %'},
{'Rank': '22', 'Change': '', 'IDE': 'Light Table', 'Share': '0.07 %', 'Trend': '-0.0 %'},
{'Rank': '23', 'Change': '', 'IDE': 'MonoDevelop', 'Share': '0.06 %', 'Trend': '-0.0 %'},
{'Rank': '24', 'Change': '', 'IDE': 'SharpDevelop', 'Share': '0.03 %', 'Trend': '-0.0 %'},
{'Rank': '25', 'Change': '', 'IDE': 'Eric Python', 'Share': '0.03 %', 'Trend': '-0.0 %'},
{'Rank': '26', 'Change': '', 'IDE': 'Aptana', 'Share': '0.02 %', 'Trend': '-0.0 %'},
{'Rank': '27', 'Change': '', 'IDE': 'RubyMine', 'Share': '0.02 %', 'Trend': '-0.0 %'},
{'Rank': '28', 'Change': '', 'IDE': 'Coda 2', 'Share': '0.02 %', 'Trend': '+0.0 %'},
{'Rank': '29', 'Change': '', 'IDE': 'Monkey Studio', 'Share': '0.01 %', 'Trend': '-0.0 %'},
{'Rank': '30', 'Change': '', 'IDE': 'DrJava', 'Share': '0.01 %', 'Trend': '-0.0 %'}
]
代码如下:
def parse_ide_trend(top_ide_trend):
top_ides = []
top_ide_lines = top_ide_trend.split('\n')[1:]
head = top_ide_lines[0]
columns = head.split('\t')
for row in top_ide_lines[1:]:
row = row.strip()
if row:
cells = row.split('\t')
top_ide = {}
for i in range(0, len(columns)):
column = columns[i]
cell = cells[i]
top_ide[column] = cell
top_ides.append(top_ide)
return top_ides
if __name__ == "__main__":
top_ide_trend = ...
top_ides = parse_ide_trend(top_ide_trend)
print(top_ides)
二、代码理解
2.1 直接输出看一下字符串中的格式
2.2 split("\n)
输出结果如下:
['', 'Rank\tChange\tIDE\tShare\tTrend', '1\t\tVisual Studio\t29.24 %\t+3.5 %', '2\t\tEclipse\t13.91 %\t-2.9 %','3\t\tVisual Studio Code\t12.07 %\t+3.3 %', '4\t\tAndroid Studio\t9.13 %\t-2.5 %', '5\t\tpyCharm\t8.43 %\t+0.7 %', '']
可以看到第一个和最后一个是空值
所以对于list集合top_ide_lines,只需要取下标为1往后的值
top_ide_lines = top_ide_trend.split('\n')[1:]
由于最后一个是空,所以遍历该list时要判断非空!!
2.3 split('\t')取表的head头数据
2.4 遍历split('\t')取每个值数据
只取以上划红线部分,下标为0不是值
所以需要再次用到[1:]
if __name__ == "__main__":
top_ide_trend = """
Rank Change IDE Share Trend
1 Visual Studio 29.24 % +3.5 %
2 Eclipse 13.91 % -2.9 %
3 Visual Studio Code 12.07 % +3.3 %
4 Android Studio 9.13 % -2.5 %
5 pyCharm 8.43 % +0.7 %
"""
# top_ide_lines = top_ide_trend.split("\n")
top_ide_lines = top_ide_trend.split('\n')[1:]
print(top_ide_lines)
head = top_ide_lines[0]
columns = head.split('\t')
print(columns)
top_ides = []
for row in top_ide_lines[1:]:
row = row.strip() #删除前后空格
if row: #判断非空
cells = row.split('\t')
top_ide = {}
for item in range(0,len(columns)):
column = columns[item] # item=0时,colum=Rank,作为key值
cell = cells[item] #item=0时,cell = 1,作为value值
top_ide[column] = cell # item=0时,top_ide中的值为{"Rank":"1"}
top_ides.append(top_ide)
print(top_ides)
输出结果如下:
PS E:\myProjects\lyx> & E:/Python2/python.exe e:/myProjects/lyx/test/myPython/test.py
['Rank\tChange\tIDE\tShare\tTrend', '1\t\tVisual Studio\t29.24 %\t+3.5 %', '2\t\tEclipse\t13.91 %\t-2.9 %', '3\t\tVisual Studio Code\t12.07 %\t+3.3 %', '4\t\tAndroid Studio\t9.13 %\t-2.5 %', '5\t\tpyCharm\t8.43 %\t+0.7 %', '']
['Rank', 'Change', 'IDE', 'Share', 'Trend']
[{'Rank': '1', 'Change': '', 'IDE': 'Visual Studio', 'Share': '29.24 %', 'Trend': '+3.5 %'}, {'Rank': '2', 'Change': '', 'IDE': 'Eclipse', 'Share': '13.91 %', 'Trend': '-2.9 %'}, {'Rank': '3', 'Change': '', 'IDE': 'Visual Studio Code', 'Share': '12.07 %', 'Trend': '+3.3 %'}, {'Rank': '4', 'Change': '', 'IDE': 'Android Studio', 'Share': '9.13 %', 'Trend': '-2.5 %'}, {'Rank': '5', 'Change': '', 'IDE': 'pyCharm', 'Share': '8.43 %', 'Trend': '+0.7 %'}]
PS E:\myProjects\lyx>