Ask the computer to help me write some code (script string)

Source: Internet
Author: User

WriteProgramWhen the configuration file is used, you can select Lua if Lua is more suitable.
At the beginning, all configurations are loaded from the file, and it is OK to directly call a function. Later, the program will run normally if the configuration file does not exist, so the program needs to be configured by default.
But I don't want to change the original structure. I want to hard encode the default configuration script in the program in the form of a string. In this case, I encountered a problem: the default configuration is about 6 ~ The size of 7 K. If a row is knocked, it must be very uncomfortable ......

This repetitive work should be handed over to the computer, and the final decision is to put this partCodeLet the computer write (I wrote it before, and it just copies it *_*)......
But don't write it. Well, let me write a Python code to teach it:

 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 ()

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.