New life from now
03 March, 2011
22 November, 2010
Use Regular Expression In Project
In python there is a module named re, stands for Regular Expression. I tried to use it in the project. Because the content of each lesson in code is about 10 to 40 lines, but the whole source file is a about 700 lines. So I need to extract the content to make it as normal article, easy to read, easy to translate too.
First, write the regular expression to match the content what I want:
p = re.compile(r'(?<= text=").+(?=" x=")')
then read & match them sentence by sentence:
for line in fin.readlines():
s_match = re.findall(p,line)
And write them to a new file:
if s_match:
fout.write(s_match[0]+ " ")
The new file (fout) has the whole content of this lesson which I need to translate. No matter the content is English or Chinese, it can show properly.
re.findall gets a list of strings, so if you want to use the string, just use the index of the list. If there is more than one string, and you want to read them one by one, you can use for … in s_match to get all of them.
Remember: for other languages (not English), if you read the list, you may find the strings in it were encoded in other format (not ASCII). If you want to get the string, do not use str(s_match), otherwise, you will get the encoding of the characters, such as: \xc4\x87\x56\x82.......
Read the real string elements in the list, not just convert the list to string.
Labels:
Internship Programme,
python
19 August, 2010
projecteuler.net - Q5
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
i = 1
for k in range(1,21):
if i%k > 0:
for j in range(1,21):
if (i*j) % k == 0:
i *= j
break
print i
Labels:
algorithm
02 July, 2010
算法之美:求一个数n的最大质因数(Python)
n=600851475143
d=2
while d < n/d:
if n%d==0:
n/=d
else:
d+=1
print n
Labels:
algorithm
28 April, 2010
Counting occurrences of a pattern
Use the :s substitute command with the n flag (counts number of hits; no changes occur) to count the number of occurrences of a pattern. For example:
:%s/pattern//gn
Labels:
using Vim
06 April, 2010
26 March, 2010
Restrict the input in textarea in actionscript3
Without restriction, user can input anything in the textarea.
In my internship program, users need according to the test text to input text in the textarea, after test we give them the typing speed and accuracy results.
But, user can input anything they want, even some html tags or javascript, so we need to restrict their input.
In actionscript3, there is a method of textarea to do this. For example:
That means user cannot input"<>{}@" these characters. "^" means characters after it are unacceptable. Without "^" means only characters in the my_ta.restrict are allowed.
In my internship program, users need according to the test text to input text in the textarea, after test we give them the typing speed and accuracy results.
But, user can input anything they want, even some html tags or javascript, so we need to restrict their input.
In actionscript3, there is a method of textarea to do this. For example:
var my_ta:mx.controls.TextArea;
my_ta.restrict = "^<>{}@";
That means user cannot input"<>{}@" these characters. "^" means characters after it are unacceptable. Without "^" means only characters in the my_ta.restrict are allowed.
Labels:
Internship Programme
Subscribe to:
Posts (Atom)