Source From Here
Question
I need to find "yesterday's" date in this format MMDDYY in Python. So for instance, today's date would be represented like this: 111009 I can easily do this for today but I have trouble doing it automatically for "yesterday".
How-To
Check sample code below:
Supplement
* python时间,日期,时间戳处理
This is a blog to track what I had learned and share knowledge with all who can take advantage of them
標籤
- [ 英文學習 ]
- [ 計算機概論 ]
- [ 深入雲計算 ]
- [ 雜七雜八 ]
- [ Algorithm in Java ]
- [ Data Structures with Java ]
- [ IR Class ]
- [ Java 文章收集 ]
- [ Java 代碼範本 ]
- [ Java 套件 ]
- [ JVM 應用 ]
- [ LFD Note ]
- [ MangoDB ]
- [ Math CC ]
- [ MongoDB ]
- [ MySQL 小學堂 ]
- [ Python 考題 ]
- [ Python 常見問題 ]
- [ Python 範例代碼 ]
- [心得扎記]
- [網路教學]
- [C 常見考題]
- [C 範例代碼]
- [C/C++ 範例代碼]
- [Intro Alg]
- [Java 代碼範本]
- [Java 套件]
- [Linux 小技巧]
- [Linux 小學堂]
- [Linux 命令]
- [ML In Action]
- [ML]
- [MLP]
- [Postgres]
- [Python 學習筆記]
- [Quick Python]
- [Software Engineering]
- [The python tutorial]
- 工具收集
- 設計模式
- 資料結構
- ActiveMQ In Action
- AI
- Algorithm
- Android
- Ansible
- AWS
- Big Data 研究
- C/C++
- C++
- CCDH
- CI/CD
- Coursera
- Database
- DB
- Design Pattern
- Device Driver Programming
- Docker
- Docker 工具
- Docker Practice
- Eclipse
- English Writing
- ExtJS 3.x
- FP
- Fraud Prevention
- FreeBSD
- GCC
- Git
- Git Pro
- GNU
- Golang
- Gradle
- Groovy
- Hadoop
- Hadoop. Hadoop Ecosystem
- Java
- Java Framework
- Java UI
- JavaIDE
- JavaScript
- Jenkins
- JFreeChart
- Kaggle
- Kali/Metasploit
- Keras
- KVM
- Learn Spark
- LeetCode
- Linux
- Lucene
- Math
- ML
- ML Udemy
- Mockito
- MPI
- Nachos
- Network
- NLP
- node js
- OO
- OpenCL
- OpenMP
- OSC
- OSGi
- Pandas
- Perl
- PostgreSQL
- Py DS
- Python
- Python 自製工具
- Python Std Library
- Python tools
- QEMU
- R
- Real Python
- RIA
- RTC
- Ruby
- Ruby Packages
- Scala
- ScalaIA
- SQLAlchemy
- TensorFlow
- Tools
- UML
- Unix
- Verilog
- Vmware
- Windows 技巧
- wxPython
2018年7月31日 星期二
2018年7月27日 星期五
[ Python 常見問題 ] requests - How to download large file in python?
Source From Here
Question
Requests is a really nice library. I'd like to use it for download big files (>1GB). The problem is it's not possible to keep whole file in memory I need to read it in chunks. And this is a problem with the following code:
By some reason it doesn't work this way. It still loads response into memory before save it to a file.
How-To
It's much easier if you use Response.raw and shutil.copyfileobj():
This streams the file to disk without using excessive memory, and the code is simple. For large file, you need to write the content piece by piece to avoid "out of memory":
See http://docs.python-requests.org/en/latest/user/advanced/#body-content-workflow for further reference.
Question
Requests is a really nice library. I'd like to use it for download big files (>1GB). The problem is it's not possible to keep whole file in memory I need to read it in chunks. And this is a problem with the following code:
- import requests
- def DownloadFile(url)
- local_filename = url.split('/')[-1]
- r = requests.get(url)
- f = open(local_filename, 'wb')
- for chunk in r.iter_content(chunk_size=512 * 1024):
- if chunk: # filter out keep-alive new chunks
- f.write(chunk)
- f.close()
- return
How-To
It's much easier if you use Response.raw and shutil.copyfileobj():
- import requests
- import shutil
- def download_file(url):
- local_filename = url.split('/')[-1]
- r = requests.get(url, stream=True)
- with open(local_filename, 'wb') as f:
- shutil.copyfileobj(r.raw, f)
- return local_filename
- def download_file(url):
- local_filename = url.split('/')[-1]
- # NOTE the stream=True parameter
- r = requests.get(url, stream=True)
- with open(local_filename, 'wb') as f:
- for chunk in r.iter_content(chunk_size=1024):
- if chunk: # filter out keep-alive new chunks
- f.write(chunk)
- #f.flush() commented by recommendation from J.F.Sebastian
- return local_filename
[ Python 常見問題 ] requests - How to “log in” to a website?
Source From Here
Question
I am trying to post a request to log in to a website using the Requests module in Python but its not really working.
How-To
If the information you want is on the page you are directed to immediately after login, below is the simple code:
Assuming your login attempt was successful, you can simply use the session instance to make further requests to the site. The cookie that identifies you will be used to authorize the requests:
Question
I am trying to post a request to log in to a website using the Requests module in Python but its not really working.
How-To
If the information you want is on the page you are directed to immediately after login, below is the simple code:
- payload = {'inUserName': 'USERNAME/EMAIL', 'inUserPass': 'PASSWORD'}
- url = 'http://www.locationary.com/home/index2.jsp'
- requests.post(url, data=payload)
- import requests
- # Fill in your details here to be posted to the login form.
- payload = {
- 'inUserName': 'username',
- 'inUserPass': 'password'
- }
- # Use 'with' to ensure the session context is closed after use.
- with requests.Session() as s:
- p = s.post('LOGIN_URL', data=payload)
- # print the html returned or something more intelligent to see if it's a successful login page.
- print p.text
- # An authorised request.
- r = s.get('A protected web page url')
- print r.text
- # etc...
2018年7月25日 星期三
[ Python 常見問題 ] Pandas - how do you filter pandas dataframes by multiple columns
Source From Here
Question
To filter a dataframe (df) by a single column, if we consider data with male and females we might:
Question 1 - But what if the data spanned multiple years and i wanted to only see males for 2014? In other languages I might do something like:
(except I want to do this and get a subset of the original dataframe in a new dataframe object)
Question 2. How do I do this in a loop, and create a dataframe object for each unique sets of year and gender (i.e. a df for: 2013-Male, 2013-Female, 2014-Male, and 2014-Female
How-To
Using & operator, don't forget to wrap the sub-statements with ():
To store your dataframes in a dict using a for loop:
Question
To filter a dataframe (df) by a single column, if we consider data with male and females we might:
- males = df[df[Gender]=='Male']
- if A = "Male" and if B = "2014" then
Question 2. How do I do this in a loop, and create a dataframe object for each unique sets of year and gender (i.e. a df for: 2013-Male, 2013-Female, 2014-Male, and 2014-Female
How-To
Using & operator, don't forget to wrap the sub-statements with ():
- males = df[(df[Gender]=='Male') & (df[Year]==2014)]
- from collections import defaultdict
- dic={}
- for g in ['male', 'female']:
- dic[g]=defaultdict(dict)
- for y in [2013, 2014]:
- dic[g][y]=df[(df[Gender]==g) & (df[Year]==y)] #store the DataFrames to a dict of dict
2018年7月24日 星期二
[ Python 常見問題 ] Print in terminal with colors?
Source From Here
Question
How can I output colored text to the terminal, in Python? What is the best Unicode symbol to represent a solid block?
How-To
One easier option would be to use the cprint function from termcolor package:
Or you can define a string that starts a color and a string that ends the color, then print your text with the start string at the front and the end string at the end:
This produces the following in bash, in urxvt with a Zenburn-style color scheme:
Through experemintation, we can get more colors:
This way we can create a full color collection:
Here is the code to generate the test:
Supplement
* URxvt 和 Bash 的自定义配色
Question
How can I output colored text to the terminal, in Python? What is the best Unicode symbol to represent a solid block?
How-To
One easier option would be to use the cprint function from termcolor package:
>>> from termcolor import cprint
>>> cprint('john', 'red')
john
>>> cprint('ken', 'green')
ken
>>> cprint('%s is %d years-old' % ('john', 37), 'blue')
john is 37 years-old
Or you can define a string that starts a color and a string that ends the color, then print your text with the start string at the front and the end string at the end:
- CRED = '\033[91m'
- CEND = '\033[0m'
- print(CRED + "Error, does not compute!" + CEND)
Through experemintation, we can get more colors:
This way we can create a full color collection:
- CEND = '\33[0m'
- CBOLD = '\33[1m'
- CITALIC = '\33[3m'
- CURL = '\33[4m'
- CBLINK = '\33[5m'
- CBLINK2 = '\33[6m'
- CSELECTED = '\33[7m'
- CBLACK = '\33[30m'
- CRED = '\33[31m'
- CGREEN = '\33[32m'
- CYELLOW = '\33[33m'
- CBLUE = '\33[34m'
- CVIOLET = '\33[35m'
- CBEIGE = '\33[36m'
- CWHITE = '\33[37m'
- CBLACKBG = '\33[40m'
- CREDBG = '\33[41m'
- CGREENBG = '\33[42m'
- CYELLOWBG = '\33[43m'
- CBLUEBG = '\33[44m'
- CVIOLETBG = '\33[45m'
- CBEIGEBG = '\33[46m'
- CWHITEBG = '\33[47m'
- CGREY = '\33[90m'
- CRED2 = '\33[91m'
- CGREEN2 = '\33[92m'
- CYELLOW2 = '\33[93m'
- CBLUE2 = '\33[94m'
- CVIOLET2 = '\33[95m'
- CBEIGE2 = '\33[96m'
- CWHITE2 = '\33[97m'
- CGREYBG = '\33[100m'
- CREDBG2 = '\33[101m'
- CGREENBG2 = '\33[102m'
- CYELLOWBG2 = '\33[103m'
- CBLUEBG2 = '\33[104m'
- CVIOLETBG2 = '\33[105m'
- CBEIGEBG2 = '\33[106m'
- CWHITEBG2 = '\33[107m'
- x = 0
- for i in range(24):
- colors = ""
- for j in range(5):
- code = str(x+j)
- colors = colors + "\33[" + code + "m\\33[" + code + "m\033[0m "
- print(colors)
- x=x+5
Supplement
* URxvt 和 Bash 的自定义配色
訂閱:
文章 (Atom)
[Git 常見問題] error: The following untracked working tree files would be overwritten by merge
Source From Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 # git clean -d -fx 方案2: 今天在服务器上 gi...
-
前言 : 為什麼程序管理這麼重要呢?這是因為: * 首先,本章一開始就談到的,我們在操作系統時的各項工作其實都是經過某個 PID 來達成的 (包括你的 bash 環境), 因此,能不能進行某項工作,就與該程序的權限有關了。 * 再來,如果您的 Linux 系統是個...
-
屬性 : 系統相關 - 檔案與目錄 語法 : du [參數] [檔案] 參數 | 功能 -a | 顯示目錄中個別檔案的大小 -b | 以bytes為單位顯示 -c | 顯示個別檔案大小與總和 -D | 顯示符號鏈結的來源檔大小 -h | Hum...
-
來源自 這裡 說明 : split 是 Perl 中非常有用的函式之一,它可以將一個字串分割並將之置於陣列中。若無特別的指定,該函式亦使用 RE 與 $_ 變數 語法 : * split /PATTERN/,EXPR,LIMIT * split /...