資料工廠 Faker

2021-09-17 02:58:42 字數 3553 閱讀 3005

無論是效能測試, 還是大資料的測試, 甚至基本的功能測試, 造資料都是一件棘手的事

拋開資料間的關聯關係不談, 各種型別的資料長短不一, 格式不一, 單純的複製貼上不利於識別, 也不符合一般的資料場景

有幸得知faker模組, 解決了大部分的資料構造問題

語法特別簡單, 學習參考:

蔣建山西省

湖南省建市瀋北新深圳街a座 238009

18770641692

硬體工程師構造資料的思路大體相似, 採用隨機方式

各類資料通過不同的provider實現,provider定義資料模板和具體實現

內建provider有address, color, compnay, internet, person, geo, user_agent等, 不過並不是所有型別都適用於zh_cn

我們看一下如何生成姓名

class

provider

(baseprovider)

: formats =

['} }'

] first_names =

['john'

,'jane'

] last_names =

['doe'

]def

name

(self)

:"""

:example 'john doe'

"""pattern = self.random_element(self.formats)

return self.generator.parse(pattern)..

..

class

provider

(personprovider)

: formats =

["}}"

] first_names_male =

["偉"

,"強"

,"磊"

,"洋"

,"勇"

,"軍"

,"傑"

,"濤"

,"超"

,"明"

,"剛"

,"平"

,"輝"

,"鵬"

,"華"

,"飛"

,"鑫"

,"波"

,"斌"

,"宇"

,"浩"

,"凱"

,"健"

,"俊"

,"帆"

,"帥"

,"旭"

,"寧"

,"龍"

,"林"

,"歡"

,"佳"

,"陽"

,"建華"

,"亮"

,"成"

,"建"

,"峰"

,"建國"

,"建軍"

,"晨"

,"瑞"

,"志強"

,"兵"

,"雷"

,"東"

,"博"

,"彬"

,"坤"

,"想"

,"巖"

,"楊"

,"文"

,"利"

,"楠"

,"紅霞"

,"建平",]

first_names_female =

["芳"

,"娜"

,"敏"

,"靜"

,"秀英"

,"麗"

,"豔"

,"娟"

,"霞"

,"秀蘭"

,"燕"

,"玲"

,"桂英"

,"丹"

,"萍"

,"紅"

,"玉蘭"

,"桂蘭"

,"英"

,"梅"

,"莉"

,"秀珍"

,"婷"

,"玉梅"

,"玉珍"

,"鳳英"

,"晶"

,"玉英"

,"穎"

,"雪"

,"慧"

,"紅梅"

,"倩"

,"琴"

,"蘭英"

,"暢"

,"雲"

,"潔"

,"柳"

,"淑珍"

,"春梅"

,"海燕"

,"冬梅"

,"秀榮"

,"桂珍"

,"瑩"

,"秀雲"

,"桂榮"

,"秀梅"

,"麗娟"

,"婷婷"

,"玉華"

,"琳"

,"雪梅"

,"淑蘭"

,"麗麗"

,"玉"

,"秀芳"

,"欣"

,"淑英"

,"桂芳"

,"麗華"

,"丹丹"

,"桂香"

,"淑華"

,"榮"

,"秀華"

,"桂芝"

,"小紅"

,"金鳳"

,"瑜"

,"桂花"

,"璐"

,"鳳蘭",]

first_names = first_names_male + first_names_female

...

看以看到formats定義了姓名的格式, 屬性由一系列樣本資料構成, 通過隨機方式獲取, 格式中的佔位符(last_name, first_name)則通過正規表示式完成替換

使用faker造假資料

如要建立一批使用者,建立一段文字,或者是 號碼,抑或著是街道位址或者ip位址等等。以前要麼就是鍵盤一頓亂敲,隨便造個什麼字串出來,當然最後誰也不認識誰。現在你不要這樣做了。用faker就能滿足你的一切需求。先安裝faker pip install faker複製 建立faker物件 from fak...

faker庫生成資料

faker庫用於生成偽造的資料。使用pip安裝 pip install faker設定中文 from faker import faker fk faker locale zh cn 設定多語言 from faker import faker fk faker locale zh cn en us ...

58 Faker 造假資料

以前聽說過造假資料的庫,但沒有上心,回想前段時間為造假資料還自己花不少時間寫指令碼,faker現成的庫卻不去用,真是無力吐槽 pip install fakerfrom faker import faker 例項物件 fake faker locale zh cn 中文,預設是英文 name fak...