2011/03
- CUnit Link 2011.03.18
- Saving a Python dict to a file using pickle 2011.03.18
- [python] strip() , lstrip() , rstrip() 2011.03.18
- [python] getopt 함수 2011.03.18
- PHP XML Parser 2011.03.18
CUnit Link
2011. 3. 18. 12:01
Saving a Python dict to a file using pickle
2011. 3. 18. 10:51
[출처 : http://www.saltycrane.com/blog/2008/01/saving-python-dict-to-file-using-pickle/]
Here is an example using
Results:
Here is an example using
pickle
which writes a Python dict to a file and reads it back again:import pickle
# write python dict to a file
mydict = {'a': 1, 'b': 2, 'c': 3}
output = open('myfile.pkl', 'wb')
pickle.dump(mydict, output)
output.close()
# read python dict back from the file
pkl_file = open('myfile.pkl', 'rb')
mydict2 = pickle.load(pkl_file)
pkl_file.close()
print mydict
print mydict2
Results:
{'a': 1, 'c': 3, 'b': 2} {'a': 1, 'c': 3, 'b': 2}
Maybe you want something like this?
import pickle
# write python dict to a file
mydict = {'a': 1, 'b': 2, 'c': 3}
output = open('myfile.pkl', 'wb')
pickle.dump(mydict, output)
output.close()
print mydict
# read python dict back from the file
pkl_file = open('myfile.pkl', 'rb')
mydict = pickle.load(pkl_file)
pkl_file.close()
print mydict
# update dict and write to the file again
mydict.update({'d': 4})
output = open('myfile.pkl', 'wb')
pickle.dump(mydict, output)
output.close()
# read python dict back from the file
pkl_file = open('myfile.pkl', 'rb')
mydict = pickle.load(pkl_file)
pkl_file.close()
print mydict
Results:
{'a': 1, 'c': 3, 'b': 2}
{'a': 1, 'c': 3, 'b': 2}
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
[python] strip() , lstrip() , rstrip()
2011. 3. 18. 10:44
[출처 : http://shinygirl33.tistory.com/27]
1.strip([charset])
- 문자열 양끝제거. charset 지정이 없으면 공백문자를 제거. 지정되어 있으면 chars의 모든 조합을 제거
>>> "\t python \t".strip()
'python'
>>> ">>> python is good <<<".strip('<> ')
'python is good'
1.strip([charset])
- 문자열 양끝제거. charset 지정이 없으면 공백문자를 제거. 지정되어 있으면 chars의 모든 조합을 제거
>>> "\t python \t".strip()
'python'
>>> ">>> python is good <<<".strip('<> ')
'python is good'
2. lstrip([chars])
- 문자열 왼쪽 제거. chars지정이 없으면 공백문자를 제거. 지정되어 있으면 chars의 모든 조합을 제거
>>> "\t\n python".lstrip()
'python'
>>> ">>> python is good".lstrip('>')
' python is good'
3. rstrip([chars])
- 문자열 오른쪽 제거. chars지정이 없으면 공백문자를 제거. 지정되어 있으면 chars의 모든 조합을 제거
>>> ">>> python is good <<<".rstrip('>< ')
'>>> python is good'
[python] getopt 함수
2011. 3. 18. 10:43
[출처 : http://shinygirl33.tistory.com/22]
[결과]
[명령행 옵션 처리]
getopt 모듈의 getopt 함수를 이용하면 sys.argv 로 전달받은 명령행의 인수 리스트에서 옵션을 불리해낸다.
옵션 인수는 -로 시작한다.
getopt 함수는 두개의 인수를 받는데, 첫 번째는 인수리스트(sys.argv[1:])이고, 두 번째는 옵션 문자들이다.
옵션 문자에 :가 사용된다면 옵션에 추가의 인수를 받아들인다는 의미이다.
즉, 'abc:d'에서 a,b는 단독 옵션이고 c,d는 인수를 갖는 옵션이다.
[예제]
[결과]
PHP XML Parser
2011. 3. 18. 10:37