2018年4月

import json

json_data = """{
   "favourite":{
      "bkmrk":{
         "id1490843709594066":{
            "guid":"904eff52277f403b89f6410fe2758646.11",
            "lcate":"1"
         },
         "id1490843712805183":{
            "guid":"58457f60eca64025bc43a978f9c98345.16",
            "lcate":"2"
         },
         "id149084371467327":{
            "guid":"a0f907f9dc8b40f689b083f3eba7228b.16",
            "lcate":"3"
         },
         "id1490843716295393":{
            "guid":"eb75d929455e468bb712e7bc2025d11a.16",
            "lcate":"4"
         }
      }
   }
}"""

data = json.loads(json_data)
for v in data['favourite']['bkmrk'].values():
    print("%s;%s" % (v['lcate'],  v['guid']))

eg:

#! /usr/bin/python  
import sys  
import string  
import os  
base_dir="/home/qinjianwang/mtr_dir/2012_09_12/tw"  
as_uniq_info=base_dir + "/as_uniq_info"  
get_line_num="wc -l " + as_uniq_info + " | awk '{print $1}'" ###get the lines of "as_uniq_info"  
line_num = os.popen(get_line_num).read().strip('\n')  
global VEXNUM 

VEXNUM = string.atoi(line_num)

- 阅读剩余部分 -

enumerate是python 2.3中新增的内置函数,它的英文说明为:

enumerate( iterable)

Return an enumerate object. iterable must be a sequence, an iterator,or some other object which supports iteration. The next() method ofthe iterator returned by enumerate() returns a tuple containing acount (from zero) and the corresponding value obtained from iteratingover iterable. enumerate() is useful for obtaining an indexed series:(0, seq[0]), (1, seq[1]), (2, seq[2]), .... New in version 2.3.

- 阅读剩余部分 -

通常在读写文件之前,需要判断文件或目录是否存在,不然某些处理方法可能会使程序出错。所以最好在做任何操作之前,先判断文件是否存在。

这里将介绍三种判断文件或文件夹是否存在的方法,分别使用os模块、Try语句、pathlib模块。

1.使用os模块
os模块中的os.path.exists()方法用于检验文件是否存在。

判断文件是否存在

import os
#如果存在返回True
>>>os.path.exists('test_file.txt')
>>>True
#如果不存在返回False
>>>os.path.exists('no_exist_file.txt')
>>>False

- 阅读剩余部分 -

Python中是有查找功能的,四种方式:in、not in、count、index,前两种方法是保留字,后两种方式是列表的方法。

下面以a_list = ['a','b','c','hello'],为例作介绍:

1、 判断值是否在列表中,in操作符

# 判断a是否在列表中,并返回True或False
'a' in a_list

- 阅读剩余部分 -

<?php
header('Content-Type:text/html;Charset=utf-8');
$db = new mysqli();
$db->connect('localhost','root',12345,'test');
$sql='select * from category';
$query=$db->query($sql);


while($rs=mysqli_fetch_array($query)){
    $result[]=$rs;
}
$i=0;
print_r($result);
echo '<hr>';
foreach($result as $key=>$value){
    $i+=1;
    if($i%2==0){
    echo $key.' ';
        echo $value.'<hr>';
    }
}
$query->free();
$db->close();

读写文件是最常见的IO操作。Python内置了读写文件的函数,用法和C是兼容的。

读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系统打开一个文件对象(通常称为文件描述符),然后,通过操作系统提供的接口从这个文件对象中读取数据(读文件),或者把数据写入这个文件对象(写文件)。

- 阅读剩余部分 -