程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python regularly replaces some words, updates dictionary key names, and constructs data through dictionaries

編輯:Python

First , Regular replacement of some words , This is still very useful , For example, Yao replaced a character of a person's name with *, Privacy protection , One or two manual operation is OK. If it is too much, it will be troublesome .

For example, there is a list of names

I want to quickly replace the middle character with * Number .

 

for k, v in dct230.items():
print(k)
k2 = re.sub(r"([\u4e00-\u9fa5])([\u4e00-\u9fa5])([\u4e00-\u9fa5]{0,1})", r"\1*\3", k)
print(k2)
dct230[k2] = dct230.pop(k)

stay python Inside , Solve the regularization problem by grouping , Use parentheses when grouping , When you replace it, you do it \1, \2, \3 To represent the selected content .

Replacement completed , Replace the original dictionary with the replaced key name .

use

pop Take out the original key value , A new pair of .

then , Use a dictionary to generate dataframe when , There will be mistakes . Directly generate , It will prompt for an error .

ValueError: If using all scalar values, you must pass an index

You can use :

pd.DataFrame(dct230, index=[0])
pd.DataFrame.from_dict(dct230, orient='index')
pd.DataFrame(list(dct230.itemsru ()))

All these methods are OK .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved