python常用

1 字符串中加空格
" ".join(list(user_q))

2 dic转json

hjson = json.dumps(stand_q_dic, ensure_ascii=False)

单引号变双引号
hhjson = json.dumps(hjson, separators=(',', ':'), ensure_ascii=False)

3 str转json

string
hjson = json.loads(psrser_json)
message = hjson["message"]

文件
fp = open('data_for_bigru/char_c2i_128.json', 'r+')
dict = json.load(fp)
print(dict)

4 将相同id的其他字段,放在一起

standq_dic = {}
for i in range(1, sheet1.nrows):
    query = sheet1.cell(i, 0).value
    type = sheet1.cell(i, 1).value
    standq = sheet1.cell(i, 2).value
    if type == "知识库":
        value = standq_dic.get(standq, 0)
        temp_list = []
        if value == 0:
            temp_list.append(query)
            standq_dic[standq] = temp_list
        else:
            value.append(query)
            standq_dic[standq] = value
5 dic按照key还是value排序,下例d[1]表示按照value进行排序
standq_count_dic = {}
for key in standq_dic:
    standq_count_dic[key] = len(standq_dic[key])

result = sorted(standq_count_dic.items(), key=lambda d: d[1], reverse=True)
for element in result:
    print(element)

https://blog.csdn.net/penglei0901/article/details/37494721

5 查python是32位还是64位

  1. >>> import platform

  2. >>> platform.architecture()

6 字符串前加 b

例: response = b'<h1>Hello World!</h1>'     # b' ' 表示这是一个 bytes 对象

作用:

b" "前缀表示:后面字符串是bytes 类型。

用处:

网络编程中,服务器和浏览器只认bytes 类型数据。

如:send 函数的参数和 recv 函数的返回值都是 bytes 类型

附:

在 Python3 中,bytes 和 str 的互相转换方式是
str.encode('utf-8')
bytes.decode('utf-8')

print(str(b'\xe9\x99\x88\xe5\x98\x89\xe5\xba\x9a', 'utf8'))

https://www.cnblogs.com/liangmingshen/p/9274021.html

 

7 python写excel数据

def write_excel(myWorkbook):
    mySheet0 = myWorkbook.add_sheet('data')
    f = open("end2end_answer.txt", encoding="utf-8", mode="r")
    lines = f.readlines()
    k = 0
    for line in lines:
        k += 1
        line = line.strip()
        line_list = line.split("\t")
        mySheet0.write(k, 0, line_list[0])
        mySheet0.write(k, 1, line_list[1])
        mySheet0.write(k, 2, line_list[2])
        mySheet0.write(k, 3, line_list[3])


if __name__ == '__main__':
    myWorkbook = xlwt.Workbook()
    write_excel(myWorkbook)
    myWorkbook.save('out_put.xls')

8 python读excel

 

path = "../out_put_880002.xlsx"
workbook = xlrd.open_workbook(path)
sheet1 = workbook.sheet_by_index(0)  # sheet索引从0开始
title_simquery_factor_map = {}
for i in range(0, sheet1.nrows):
    title_list = []
    standq = sheet1.cell(i, 0).value
    subject = sheet1.cell(i, 1).value.strip()
    predicate = sheet1.cell(i, 2).value.strip()
    condition = sheet1.cell(i, 3).value.strip()

9 list中每个元素出现次数

a = [1, 2, 3, 1, 1, 2]
dict = {}
for key in a:
    dict[key] = dict.get(key, 0) + 1
print(dict)
10 两个迭代器
alist = ['a1', 'a2', 'a3'] 
blist = ['b1', 'b2', 'b3'] 
for i, (a, b) in enumerate(zip(alist, blist)): 
print(i, a, b)
https://www.cnblogs.com/everfight/p/enumerate_with_zip.html

11 csv的操作

import csv

with open('test.csv')as f:

    f_csv = csv.reader(f)

    for row in f_csv:

         print(row[1])

https://blog.csdn.net/katyusha1/article/details/81606175

12 numpy输出所有值

np.set_printoptions(threshold=np.inf)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值