This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| import time | |
| # timestamp to datetime | |
| ts = int(time.time()) | |
| print datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') | |
| # datetime to timestamp | |
| print datetime.datetime(2017, 05, 23, 10, 30, 00).strftime('%s') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| file_ = open('file.csv', 'r') | |
| reader = csv.reader(file_, delimiter=',', | |
| quotechar='|', quoting=csv.QUOTE_MINIMAL) | |
| file_ = open('file.csv', 'aw') | |
| writer = csv.writer(file_, delimiter=',', | |
| quotechar='|', quoting=csv.QUOTE_MINIMAL) | |
| file_.flush() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| # 去除所有半角全角符号,只留字母、数字、中文。 | |
| def remove_punctuation(line): | |
| rule = re.compile(ur'[^a-zA-Z0-9\u4e00-\u9fa5]') | |
| line = rule.sub('',line) | |
| return line |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # coding: utf-8 | |
| from tornado.web import RequestHandler | |
| class UploadHandler(RequestHandler): | |
| def put(self): | |
| username = self.get_body_argument('username') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # INSERT INTO table_tags (tag) VALUES ('tag_a'),('tab_b'),('tag_c') ON DUPLICATE KEY UPDATE tag=tag; | |
| from sqlalchemy.ext.compiler import compiles | |
| from sqlalchemy.sql.expression import Insert | |
| @compiles(Insert) | |
| def append_string(insert, compiler, **kw): | |
| s = compiler.visit_insert(insert, **kw) | |
| if 'append_string' in insert.kwargs: | |
| return s + " " + insert.kwargs['append_string'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### 保存图片到内存 | |
| import StringIO | |
| output = StringIO.StringIO() | |
| format = 'PNG' # or 'JPEG' or whatever you want | |
| image.save(output, format) | |
| contents = output.getvalue() | |
| output.close() | |
| ### 保存图片到文件 |
OlderNewer