程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Use Python and Sed create simple C code

編輯:Python

My Simple code is:

switch(event)
{
case EVENT_XXXX:
name = "EVENT_XXXX";
break;
}

And all the events are defined on a web page as:

<td class="tg-cly1">EVENT_XXXX(OtaStateId,&nbsp;&nbsp;&nbsp;OK/FAIL)</td>
<td class="tg-cly1">EVENT_YYYY</td>
<td class="tg-cly1">EVENT_OOOO(PwrModeId)</td>
<td class="tg-cly1">EVENT_PPPP(PwrModeId)</td>

And there are many events and they might be changed in the feature. If I manucally type it, looks stupid.

So first step is save the web page to a file webpage.txt:

Second step is get all the lines of EVENT_XXXX

grep EVENT webpage.txt | sort > temp.txt

then remove head and tail as following.

sed 's/<\/td>//' temp.txt | sed 's/<.*>//'| sed 's/(\(.*\))//' > temp4.txt

first sed remove end </td>

second sed remove head <td class="tg-cly1">

last sed remove (xxxx)

Then I got temp4.txt which has only EVENT_XXXX

Now we can create the simple C code by python.

#!/bin/python
f=open("temp4.txt")
for eachline in f:
event = eachline.strip('\n')
event = event.lstrip()
print("case %s: name=\"%s\";break;\n" % (event,event))

for each line, remove left spaces and right enter. 

then I can get the simple C code.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved