讓電腦幫我寫點代碼(指令碼字串)

來源:互聯網
上載者:User

寫程式用到了設定檔,感覺lua比較適合,就選lua了。
開始時所有配置都從檔案載入,直接調用一個函數就OK了,後來考慮到如果設定檔不存在的話程式也要正常運行,所以程式需要預設配置。
可我不想改變原來的結構,考慮將預設的配置指令碼以字串的形式寫入程式碼在程式裡面,這時遇到一個問題 :預設配置大概6~7K的大小,如果一行行的敲,那感覺肯定是非常不爽……

這重複性的勞動就交給電腦做吧,最後決定將這部分代碼讓電腦寫(我之前寫過了,它照著抄就是了 *_*)……
可它不寫啊,那好吧,我寫個python代碼教它吧:

 1 #! /usr/bin/python
2 # fileName : writeCode.py
3 # Author : Mike
4 # E-Mail : Mike_Zhang@live.com
5
6 import string
7
8 endLinePos = -1 # windows '\n'
9 #endLinePos = -2 # Linux '\r\n'
10
11 fin = file("cnf.lua","r")
12 fout = file("code.cpp","w")
13 srcStr = '\"'
14 dstStr = '\\\"'
15 varNameStr = "luaScriptStr"
16
17 for line in fin.readlines() :
18 if not line.split() : continue
19 tmp2 = ''
20 tmp2 += varNameStr
21 tmp2 += ' += \"'
22
23 if line.count(srcStr) > 0 :
24 nStartPos = 0
25 nPos = 0
26 while True :
27 nPos = line.find(srcStr,nStartPos)
28 if nPos == -1 :
29 tmp2 += line[nStartPos:endLinePos:]
30 break
31 tmp2 += line[nStartPos:nPos:]
32 tmp2 += dstStr
33 nStartPos = nPos+len(srcStr)
34 else:
35 tmp2 += line[:endLinePos:]
36 tmp2 += '\\n\";\n'
37 print tmp2,
38 fout.write(tmp2)
39 fout.close()
40 fin.close()
41 raw_input()

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.