复制指定文件到目标目录

1
2
3
4
import shutil
source_path = '/source/file/full/path'
target_path = '/target/directory/full/path'
shutil.copy(source_path, target_path)

图片转换成Base64

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import base64


def pic_to_base64(pic_full_path):
with open(pic_full_path, "rb") as f:
# b64encode是编码,b64decode是解码
base64_data = base64.b64encode(f.read())
# base64.b64decode(base64data)

_base64_str = 'data:image/jpg;base64,%s' % base64_data.decode()
# print(_base64_str)
return _base64_str


if __name__ == '__main__':
base64_str = pic_to_base64('/your/picture/full/path')
print(base64_str)

从文件中读取地址,然后在浏览器中把地址标出来

  • 需要用到的文件有:

    • address.txt 这是保存原始地址的文件
    • get_locations.py 这个文件是从address.txt文件读取地址,然后转换成经纬度,最后生成一个js文件保存经纬度信息
    • location_generated.js 这个文件是get_locations.py生成的
    • map.html 这个是展示地图的文件,只要双击打开就行
  • address.txt文件示例:

    1
    2
    3
    4
    5
    6
    合肥庐阳区新天地国际中心
    合肥庐阳区和煦园
    合肥财富广场
    合肥大剧院
    合肥体育馆
    合肥安医附院
  • get_locations.py文件的内容

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    import json
    import requests # 这是第三方模块,需要安装,可以使用`pip install requests`命令安装


    def get_response(addr_file):
    """
    通过高德地图提供的接口将地址转换成经纬度坐标
    接口返回数据实例(2019/2/28):
    '{"status":"1","count":"1","info":"OK","infocode":"10000","suggestion":{"keywords":[],"cities":[]},"pois":[{"id":"B0FFG1NA8U","parent":[],"name":"合肥顶峰数码科技有限公司","type":"公司企业;公司;公司","typecode":"170200","biz_type":[],"address":"北一环路168号新天地国际购物中心7层","location":"117.279240,31.879880","tel":[],"distance":[],"biz_ext":{"rating":[],"cost":[]},"pname":"安徽省","cityname":"合肥市","adname":"庐阳区","importance":[],"shopid":[],"shopinfo":"0","poiweight":[],"photos":[]}]}'
    :param addr_file: 包含地址的文本文件,一行只能有一个地址
    :return: 返回一个生成器,迭代这个生成器会返回包含经纬度和地址的json字符串,示例:'{"lnglat": [117.27924, 31.87988], "name": "合肥庐阳区新天地国际中心", "style": 0}'
    """
    for addr in open(addr_file, 'r', encoding='utf8'):
    addr = addr.replace('\n', '')
    url = 'https://restapi.amap.com/v3/place/text?key=8325164e247e15eea68b59e89200988b&keywords=%{}'.format(addr)
    resp = json.loads(requests.get(url).text)
    location = resp['pois'][0]['location'].split(',')
    location_list = list(map(lambda x:float(x), location))
    my_dic = dict(
    lnglat=location_list,
    name=addr,
    style=0,
    )
    yield json.dumps(my_dic, ensure_ascii=False)


    def write_js_file(addr_file):
    with open('location_generated.js', 'w', encoding='utf8') as f:
    f.write('var locations = [\n')
    for addr_str in get_response(addr_file):
    f.write(''.join([' ', addr_str, ',\n']))
    f.write('];\n')


    if __name__ == '__main__':
    write_js_file('address.txt')
  • 生成的 location_generated.txt 文件示例:

    1
    2
    3
    4
    5
    6
    7
    8
    var locations = [
    {"lnglat": [117.27924, 31.87988], "name": "合肥庐阳区新天地国际中心", "style": 0},
    {"lnglat": [117.27924, 31.87988], "name": "合肥庐阳区和煦园", "style": 0},
    {"lnglat": [117.27924, 31.87988], "name": "合肥财富广场", "style": 0},
    {"lnglat": [117.27924, 31.87988], "name": "合肥大剧院", "style": 0},
    {"lnglat": [117.27924, 31.87988], "name": "合肥体育馆", "style": 0},
    {"lnglat": [117.27924, 31.87988], "name": "合肥安医附院", "style": 0},
    ];
  • map.html 文件的内容,从高德地图拷贝后简要修改,源地址:高德地图加载海量点

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>海量点</title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
    <style>
    html, body, #container {
    height: 100%;
    width: 100%;
    }

    .input-card .btn {
    margin-right: 1.2rem;
    width: 9rem;
    }

    .input-card .btn:last-child {
    margin-right: 0;
    }
    </style>
    </head>
    <body>
    <div id="container" class="map" tabindex="0"></div>
    <div class="input-card">
    <h4>海量点效果切换</h4>
    <div class="input-item">
    <input type="button" class="btn" value="单一图标" onclick='setStyle(0)'/>
    <input type="button" class="btn" value="多个图标" onclick='setStyle(1)'/>
    </div>
    </div>
    <!-- 数据源 src是生成的文件的名字 变量locations存了展示的所有数据 -->
    <script type="text/javascript" src='location_generated.js'></script>
    <script type="text/javascript"
    src="https://webapi.amap.com/maps?v=1.4.13&key=您申请的key值"></script>
    <script type="text/javascript">
    var map = new AMap.Map('container', {
    zoom: 4,
    center: [102.342785, 35.312316]
    });

    var style = [{
    url: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_bs.png',
    anchor: new AMap.Pixel(6, 6),
    size: new AMap.Size(19, 33)
    }, {
    url: 'https://a.amap.com/jsapi_demos/static/images/mass1.png',
    anchor: new AMap.Pixel(6, 6),
    size: new AMap.Size(11, 11)
    }, {
    url: 'https://a.amap.com/jsapi_demos/static/images/mass2.png',
    anchor: new AMap.Pixel(6, 6),
    size: new AMap.Size(11, 11)
    }
    ];

    var mass = new AMap.MassMarks(locations, {
    opacity: 0.8,
    zIndex: 111,
    cursor: 'pointer',
    style: style
    });

    var marker = new AMap.Marker({content: ' ', map: map});

    mass.on('mouseover', function (e) {

    marker.setPosition(e.data.lnglat);
    marker.setLabel({content: e.data.name})
    });

    mass.setMap(map);

    function setStyle(multiIcon) {
    if (multiIcon) {
    mass.setStyle(style);
    } else {
    mass.setStyle(style[2]);
    }
    }
    </script>
    </body>
    </html>

最后附上效果图(放大后截取一部分):
示例图片