• 仅楼主可见
  • 可以呀

  • import json
    import requests
    regions = ['Asia','Europe','MEA','New+Zealand','United+States']
    hotels = []
    for region in regions:
        url = "https://www.millenniumhotels.com/api/search/destinations?keywords=&regionName=%s" % region
        get_response = requests.get(url)
        if get_response.status_code == 200:
            # print(get_response.text)
            result = json.loads(get_response.text)
            hotelMsgs = result.get('data').get('hotels')
            for hotelMsg in hotelMsgs:
                hotels.append(hotelMsg.get('name'))
    [print(hotel) for hotel in hotels]
    
  • 《python 学习手册》《Python cookbook》

  • 是的,你可以对文件格式进行限制

    def getfile(path):
        paths = []
        for root, dirs, files in os.walk(path):
            for file in files:
                if os.path.splitext(file)[1] == '.jpg':
                    paths.append(os.path.join(root,file))
        return paths
    

    如果是复制的话,把shutil.move() 改成shutil.copy() 即可

  • 这样写 可以不

    # -*- coding: utf-8 -*-
    import os,shutil
    
    def movefile(srcfile,dstfile):
        fpath,fname=os.path.split(srcfile)
        if os.path.isfile(os.path.join(dstfile,fname)):
            print("%s exist!"%str(os.path.join(dstfile,fname)))
        elif not os.path.isfile(srcfile):
            print("%s not exist!")%(srcfile)
        else:
            fpath,fname=os.path.split(dstfile)
            if not os.path.exists(fpath):
                os.makedirs(fpath)
            shutil.move(srcfile,dstfile)
    
    def getfile(path):
        paths = []
        for root, dirs, files in os.walk(path):
            for file in files:
                paths.append(os.path.join(root,file))
        return paths
    
    def main():
        path = "/path/A"
        pathto = "/path/B"
        paths = getfile(path)
        for pathfrom in paths:
            print(pathfrom)
            movefile(pathfrom,pathto)
    
    if __name__ == '__main__':
        main()
    
  • 做两次二分查找

    
    def binarySearch(testList):
        if testList[0] >0 or testList[-1] < 0 or len(testList)< 3:
            print "error"
        midFlag1 = len(testList)/2
        midFlag2 = len(testList)/2
        reArray = []
        while(1):
            if testList[midFlag1] == 0 and testList[midFlag1 - 1] < 0:
                print "item before first 0 is : %s" % testList[midFlag1 - 1]
                reArray.append(testList[midFlag1 - 1])
                break
            if testList[midFlag1] >= 0 and midFlag1 > 0:
                print(testList[midFlag1],midFlag1)
                midFlag1 = midFlag1 / 2
            if testList[midFlag1] < 0 and midFlag1 <len(testList) :
                print(testList[midFlag1],midFlag1)
                midFlag1 = (len(testList) -midFlag1)/2 + midFlag1
        while(1):
            if testList[midFlag2] == 0 and testList[midFlag2 + 1] > 0:
                print "item After last 0 is : %s" % testList[midFlag2 + 1]
                reArray.append(testList[midFlag2 + 1])
                break
            if testList[midFlag2] > 0 and midFlag2 > 0:
                print(testList[midFlag2],midFlag2)
                midFlag2 = midFlag2 / 2
            if testList[midFlag2] <= 0 and midFlag2 <len(testList) :
                print(testList[midFlag2],midFlag2)
                midFlag2 = (len(testList) -midFlag2)/2 + midFlag2
        return reArray
    def main():
        testList = [-4,-2,-1,0,0,0,0,0,3,4,5]
        # testList = [-1,0,1]
        Result = binarySearch(testList)
    
    if __name__ == '__main__':
        main()
    
  • #!/bin/sh
    while true
    do
    time=$(date "+%Y-%m-%d %H:%M:%S")
    echo "${time}" >> cpu_mem.txt
    top -b -n 1 |grep -E "CPU:|Mem:" >> cpu_mem.txt
    sleep 1
    done

    用来监控 cpu memory

  • str = '大家都在搜 “我的世界”'
    print str
    大家都在搜 “我的世界”
    import re
    re = re.compile(r'.“(.)”')
    find = re.findall(str)
    print find[0]
    我的世界

  • 应该是要正则\w* 排除空,特殊字符,数字这些,再用 len