简体   繁体   中英

Python Faker - Using CSV files to create custom providers

I am relatively new to python, but do have a programming background. I would like to use the Faker package to generate fake data for a project I am working on, however, I need to be able to import my own lists for Faker to use when generating certain dependant columns.

For example, I need to be able to import my own Postal Code list as I am in Australia and the postal code list in Faker uses American style zip codes. I have searched and searched for a way to import custom lists using a CSV file as a data source to override the postal code provider hard-coded into the Faker package, but I have not been able to find anything. Every time I search, I get the opposite of what I want (ie. I get results showing how to export the generated Faker data into an CSV file).

Can someone please help me out with some basic code that would show how to import Faker package, then import a CSV of post codes (or any type of data) that can be used to override a Faker provider list?

I am at a total loss where to begin because I can't find any examples of this and I'm new to python.

Giddy, based on the comments, you could do something like this for the Aussie fake data,

from faker import Faker
fake = Faker(['en_AU']) #load the AU provider

for i in range(5):
     print(fake.address(), end='\n\n')
     #fake.postcode() for postcodes

Output:

Level 5 7 Douglas Motorway
Jenkinsshire, NSW, 8053

24 Gardner Deviation
St. Jasonhaven, NT, 0953

6/0 Michael Landing
West Johnshire, SA, 2697

1/79 Kendra Siding
Brendamouth, VIC, 2920

3/96 Amanda Part
Port Adamland, ACT, 2602

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM