Python伪造库

冒牌货 是一个Python包,可以为您生成假数据。

null

安装: 帮助链接 打开Anaconda提示符命令进行安装:

conda install -c conda-forge faker 

进口包装

 from faker import Faker

Faker能够打印/获取大量不同的假数据,例如,它可以打印假姓名、地址、电子邮件、文本等。

最常用的重要伪造命令

fake.name()
fake.address()
fake.email()
fake.text()
fake.country()

from faker import Faker
fake = Faker()
print (fake.email())
print (fake.country())
print (fake.name())
print (fake.text())
print (fake.latitude(), fake.longitude())
print (fake.url())


OUTPUT:(Different every time)
vwilson@hotmail.com
Belgium
Shane Hunter
Commodi vel libero placeat quibusdam odio odio consequatur. Ducimus libero quae optio non quidem. Facilis quas impedit quo.
26.5687745 -124.802165
http://www.turner.com/

应用1:创建一个名为students的100名学生的json。包含学生姓名、地址、位置坐标和学生学籍号的json。

from faker import Faker
# To create a json file
import json
# For student id
from random import randint
fake = Faker()
def input_data(x):
# dictionary
student_data = {}
for i in range ( 0 , x):
student_data[i] = {}
student_data[i][ 'id' ] = randint( 1 , 100 )
student_data[i][ 'name' ] = fake.name()
student_data[i][ 'address' ] = fake.address()
student_data[i][ 'latitude' ] = str (fake.latitude())
student_data[i][ 'longitude' ] = str (fake.longitude())
print (student_data)
# dictionary dumped as json in a json file
with open ( 'students.json' , 'w' ) as fp:
json.dump(student_data, fp)
def main():
# Enter number of students
# For the above task make this 100
number_of_students = 10
input_data(number_of_students)
main()
# The folder or location where this python code
# is save there a students.json will be created
# having 10 students data.


OUTPUT 
{0: {'id': 20, 'name': 'Benjamin Washington', 'address': 'USCGC GarrisonFPO AP 48025-9793', 'latitude': '-68.975800', 'longitude': '153.009590'}, 1: {'id': 2, 'name': 'Christopher Howell', 'address': '7778 Sarah Center Apt. 663Lawrenceport, WY 78084', 'latitude': '-21.8141675', 'longitude': '-122.830387'}, 2: {'id': 67, 'name': 'Fernando Fuentes', 'address': '7756 Bradford Plain Suite 997East Chelseaburgh, KY 75776', 'latitude': '-82.791227', 'longitude': '-42.964122'}, 3: {'id': 86, 'name': 'Patrick Torres', 'address': 'Unit 5217 Box 7477DPO AE 82354-0160', 'latitude': '34.949096', 'longitude': '121.715387'}, 4: {'id': 11, 'name': 'James Hines', 'address': '4567 Donald GroveWilliamhaven, MO 85891', 'latitude': '86.7208035', 'longitude': '-48.103935'}, 5: {'id': 33, 'name': 'James Miller', 'address': 'PSC 2613, Box 7165APO AP 29256-6576', 'latitude': '-35.4630595', 'longitude': '-50.415667'}, 6: {'id': 76, 'name': 'Randall Fuller', 'address': '7731 Garcia PikeNew Eric, KS 20545', 'latitude': '12.198124', 'longitude': '126.720134'}, 7: {'id': 49, 'name': 'Ivan Franco', 'address': '801 Chambers LightWest Daniel, IA 17114-4374', 'latitude': '-58.2576055', 'longitude': '171.773233'}, 8: {'id': 75, 'name': 'Amy Smith', 'address': '995 Luna Stream Apt. 297Thompsonchester, NY 82115', 'latitude': '80.4262245', 'longitude': '115.142004'}, 9: {'id': 38, 'name': 'Danielle Thomas', 'address': '7309 Chris Ferry Suite 674Colebury, MA 39673-2967', 'latitude': '-73.340443', 'longitude': '-176.964241'}}

应用2:用印地语打印10个假名字和国家。

from faker import Faker
#'hi_IN' changed the language
fake = Faker( 'hi_IN' )
for i in range ( 0 , 10 ):
print ( 'Name->' , fake.name(),
'Country->' , fake.country())


图片[1]-Python伪造库-yiteyi-C++库

应用3:创建假个人资料

import faker from Faker
fake = Faker()
print (fake.profile())


OUTPUT
{'job': 'Town planner', 'company': 'Martinez-Clark', 'ssn': '559-93-0521', 'residence': '46820 Johnny CirclesStokesside, IL 87065-2470', 'current_location': (Decimal('83.5271055'), Decimal('43.705455')), 'blood_group': 'A+', 'website': ['https://www.taylor.com/'], 'username': 'hsmith', 'name': 'Christopher Davis', 'sex': 'M', 'address': '335 Mcdaniel Fork Suite 589Teresabury, AZ 85283', 'mail': 'kenneth48@yahoo.com', 'birthdate': '1981-03-29'}

应用4:再次获取特定虚假数据的生成器种子。 种子设定使用在该种子编号处第一次生成的相同假数据结果。 实例

from faker import Faker
fake = Faker()
fake.seed( 1 )
print (fake.name())
print (fake.address())
print (fake.email())


OUTPUT
Ryan Gallagher
7631 Johnson Village Suite 690
Adamsbury, NC 50008
bparks@johnson.info

注意:即使我一次又一次地运行程序,我也会得到相同的结果。只要我去掉那个假货。种子(1)行,我们看到数据生成中的随机性。

应用5:打印所需列表中的数据。

import faker from Faker
fake = Faker()
# Print random sentences
print (fake.sentence())
# List has words that we want in our sentence
word_list = [ "GFG" , "Geeksforgeeks" ,
"shaurya" , "says" , "Gfg" ,
"GEEKS" ]
# Let's print 5 sentences that
# have words from our word_list
for i in range ( 0 , 5 ):
# You need to use ext_word_list = listnameyoucreated
print (fake.sentence(ext_word_list = word_list))


OUTPUT
# This is the random sentence that is generated using
# fake.sentence()
Error architecto inventore aut.

# These are the 5 sentence that contains words from 
# word_list we provided
Shaurya shaurya GEEKS Geeksforgeeks.
Gfg shaurya Geeksforgeeks GFG Gfg GFG.
Geeksforgeeks Gfg says Geeksforgeeks GEEKS Gfg Gfg GFG.
Geeksforgeeks shaurya GFG Geeksforgeeks Gfg GEEKS.
Gfg Geeksforgeeks says GFG GEEKS says.

总结我们从Faker那里学到的东西 1.虚假数据生成,如姓名、地址、电子邮件、文本、句子等 2.创建假数据的JSON文件。 3.打印不同语言的虚假数据。 4.创建配置文件 5.播种,即打印特定的虚假数据 6.生成包含我们提供的单词的句子。

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享