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.youkuaiyun.com/penglei0901/article/details/37494721
5 查python是32位还是64位
-
>>> import platform
-
>>> 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.youkuaiyun.com/katyusha1/article/details/81606175
12 numpy输出所有值
np.set_printoptions(threshold=np.inf)