<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                {% raw %} # Python Faker 教程 > 原文: [http://zetcode.com/python/faker/](http://zetcode.com/python/faker/) Python Faker 教程展示了如何使用 Faker 包在 Python 中生成偽數據。 我們使用`joke2k/faker`庫。 ## Faker Faker 是一個生成偽造數據的 Python 庫。 Faka 數據通常用于測試或用一些偽數據填充數據庫。 Faker 受 PHP 的 Faker,Perl 的`Data::Faker`和 Ruby 的 Faker 的啟發。 ## 設置 Faker 該包隨 composer 一起安裝。 ```py $ pip install Faker ``` 我們安裝了 Faker 模塊。 ```py $ pip install Dumper ``` 另外,我們安裝了 Dumper,它在轉儲變量時提供更好的控制臺輸出。 ## Faker 生成器 `faker.Faker()`創建并初始化一個偽造的生成器,該偽造器可以通過訪問以數據類型命名的屬性來生成數據。 Faker 將數據生成委托給提供者。 默認供應器使用英語語言環境。 Faker 支持其他語言環境; 他們的完成水平不同。 ## 簡單的 Faker 以下示例是 Faker 的簡單演示。 `simple.py` ```py #!/usr/bin/env python from faker import Faker faker = Faker() print(f'name: {faker.name()}') print(f'address: {faker.address()}') print(f'text: {faker.text()}') ``` 該示例輸出假名稱,地址和文本。 ```py $ ./simple.py name: Arthur Patton address: 0638 Larsen Way Tylermouth, CA 48344 text: Save general start bar. Become class both bank Mrs so myself. Each difficult performance even. Total anyone develop her raise research both. Laugh sport necessary tree. As during day up fall. ``` 這是一個示例輸出。 ## Faker 名稱 在第二個示例中,我們偽造與用戶名有關的數據。 `names.py` ```py #!/usr/bin/env python from faker import Faker faker = Faker() print(f'Name: {faker.name()}') print(f'First name: {faker.first_name()}') print(f'Last name: {faker.last_name()}') print('--------------------------') print(f'Male name: {faker.name_male()}') print(f'Female name: {faker.name_female()}') ``` 該示例創建假的全名,男性的姓氏和姓氏。 ```py $ ./names.py Name: Tara Brown First name: Stephanie Last name: Martin -------------------------- Male name: John Banks Female name: Lacey Roberts ``` 這是一個示例輸出。 ## Faker 工作 作業由`job()`生成。 `jobs.py` ```py #!/usr/bin/env python from faker import Faker faker = Faker() for _ in range(6): print(faker.job()) ``` 該示例創建了六個偽造作業。 ```py $ ./jobs.py Town planner Paediatric nurse Geographical information systems officer Water quality scientist Engineer, maintenance Designer, ceramics/pottery ``` 這是一個示例輸出。 ## Faker 語言環境數據 Faker 在某種程度上支持本地化數據。 語言環境被傳遞給構造方法。 請注意,語言環境已完成各種級別。 `localized.py` ```py #!/usr/bin/env python from faker import Faker faker = Faker('cz_CZ') for i in range(3): name = faker.name() address = faker.address() phone = faker.phone_number() print(f'{name}, {address}, {phone}') ``` 該示例使用捷克語生成偽造數據。 ```py $ ./localized.py Irena Novotná, Nad ?ancemi 725 055 80 Bojkovice, 606 136 053 Stanislav Svoboda, B?ezanova 225 621 17 Byst?ice pod Hostynem, 727 057 251 Klára Moravcová, Neu?ilova 6/3 134 97 ?eská Kamenice, 606 374 469 ``` 這是一個示例輸出。 請注意,捷克語具有口音。 ## Faker 貨幣 以下示例為貨幣創建偽造數據。 `currencies.py` ```py #!/usr/bin/env python from faker import Faker faker = Faker() print(f'currency: {faker.currency()}') print(f'currency name: {faker.currency_name()}') print(f'currency code: {faker.currency_code()}') ``` 該程序生成假貨幣。 ```py $ ./currencies.py currency: ('ISK', 'Icelandic króna') currency name: Israeli new shekel currency code: DJF ``` 這是一個示例輸出。 ## 假話 Faker 允許創建假單詞。 `words.py` ```py #!/usr/bin/env python from faker import Faker faker = Faker() print(f'a word: {faker.word()}') print(f'six words: {faker.words(6)}') words = ['forest', 'blue', 'cloud', 'sky', 'wood', 'falcon'] print(f'customized unique words: {faker.words(3, words, True)}') ``` 該示例創建偽單詞。 ```py print(f'a word: {faker.word()}') ``` 該行生成一個偽造的單詞。 ```py print(f'six words: {faker.words(6)}') ``` 在這里,我們生成六個偽單詞。 ```py words = ['forest', 'blue', 'cloud', 'sky', 'wood', 'falcon'] print(f'customized unique words: {faker.words(3, words, True)}') ``` 我們還可以從預定義的單詞列表中創建假單詞。 ```py $ words.py a word: show six words: ['value', 'its', 'those', 'wish', 'enter', 'hold'] customized unique words: ['forest', 'blue', 'sky'] ``` 這是一個示例輸出。 ## Faker 個人數據 Faker 可以使用`simple_profile()`創建簡單的虛擬配置文件,并使用`profile()`創建擴展的配置文件。 `profiles.py` ```py #!/usr/bin/env python from faker import Faker import dumper faker = Faker() profile1 = faker.simple_profile() dumper.dump(profile1) print('--------------------------') profile2 = faker.simple_profile('M') dumper.dump(profile2) print('--------------------------') profile3 = faker.profile(sex='F') dumper.dump(profile3) ``` 該示例為男性和女性創建虛擬概要文件。 ```py $ ./profiles.py <dict at 0x20d590a7678>: username: 'gmorgan' name: 'Jessica Clark' sex: 'F' address: '694 Joseph Valleys\nJohnfort, ME 81780' mail: 'bettybuckley@yahoo.com' birthdate: <str at 0x20d5bcbd7b0>: 'datetime.date(1938, 9, 18)' -------------------------- <dict at 0x20d5b0065e8>: username: 'mrios' name: 'Harold Wagner' sex: 'M' address: '26439 Robinson Radial\nWest Meghanmouth, PA 85463' mail: 'josechoi@gmail.com' birthdate: <str at 0x20d5bcbd7b0>: 'datetime.date(1986, 8, 18)' -------------------------- <dict at 0x20d5bd24990>: job: 'Engineer, communications' company: 'Jackson-Willis' ssn: '430-41-6118' residence: 'USNS Odom\nFPO AP 47095' current_location: <tuple at 0x20d5bca9a88> 0: <str at 0x20d5bd248a0>: "Decimal('74.1885785')" 1: <str at 0x20d5bd248a0>: "Decimal('119.951995')" blood_group: 'O-' website: ['http://roberson.com/'] username: 'ygutierrez' name: 'Lindsay Walker' sex: 'F' address: '22968 Beverly Road Suite 918\nTimothyborough, SD 10494' mail: 'aliciamccall@yahoo.com' birthdate: <str at 0x20d5bcbd7b0>: 'datetime.date(1979, 1, 4)' ``` 這是一個示例輸出。 ## Faker 號碼 Faker 允許生成隨機數字和整數。 `fake_numbers.py` ```py #!/usr/bin/env python from faker import Faker faker = Faker() print(f'Random int: {faker.random_int()}') print(f'Random int: {faker.random_int(0, 100)}') print(f'Random digit: {faker.random_digit()}') ``` 該示例生成隨機數字和整數。 ```py print(f'Random int: {faker.random_int(0, 100)}') ``` 我們可以在`random_int()`方法中指定界限。 ```py $ ./fake_numbers.py Random int: 5181 Random int: 91 Random digit: 9 ``` 這是一個示例輸出。 ## Faker 哈希和 uuid 虛假哈希和 uuid 的 Faker 支持。 `hash_uuid.py` ```py #!/usr/bin/env python from faker import Faker faker = Faker() print(f'md5: {faker.md5()}') print(f'sha1: {faker.sha1()}') print(f'sha256: {faker.sha256()}') print(f'uuid4: {faker.uuid4()}') ``` 該示例生成三個假哈希和一個 uuid 值。 ```py $ ./hash_uuid.py md5: aace57d56794686acec9eb190d401d46 sha1: 9f8f6af3089e7b5ed571591701afcfd9c2bb7a0e sha256: 8b117b58599809f50735c701f598312b0f64203a8ffacde09af23db69cfd62d5 uuid4: 38092dcd-0e49-4ac0-b39b-7edf6db62290 ``` 這是一個示例輸出。 ## Faker 互聯網相關數據 Faker 有多個用于偽造與互聯網相關的數據的訪問器。 `internet.py` ```py #!/usr/bin/env python from faker import Faker faker = Faker() print(f'Email: {faker.email()}') print(f'Safe email: {faker.safe_email()}') print(f'Free email: {faker.free_email()}') print(f'Company email: {faker.company_email()}') print('------------------------------------') print(f'Host name: {faker.hostname()}') print(f'Domain name: {faker.domain_name()}') print(f'Domain word: {faker.domain_word()}') print(f'TLD: {faker.tld()}') print('------------------------------------') print(f'IPv4: {faker.ipv4()}') print(f'IPv6: {faker.ipv6()}') print(f'MAC address: {faker.mac_address()}') print('------------------------------------') print(f'Slug: {faker.slug()}') print(f'Image URL: {faker.image_url()}') ``` 該示例顯示了各種與互聯網相關的數據,包括電子郵件,域名,信息,IP 地址和 URL。 ```py $ ./internet.py Email: hescobar@acevedo.info Safe email: jonesgregory@example.net Free email: zchambers@yahoo.com Company email: paulbailey@gordon-woods.com ------------------------------------ Host name: desktop-12.rodriguez-underwood.com Domain name: henry.com Domain word: davis TLD: com ------------------------------------ IPv4: 192.31.48.26 IPv6: 75cd:2c43:37f5:774c:dd:5a2f:ae5d:bfc9 MAC address: 3d:b1:39:ec:c6:53 ------------------------------------ Slug: of-street-fight Image URL: https://placeimg.com/311/871/any ``` 這是一個示例輸出。 ## Faker 日期和時間 Faker 有很多偽造日期和時間值的方法。 `date_time.py` ```py #!/usr/bin/env python from faker import Faker faker = Faker() print(f'Date of birth: {faker.date_of_birth()}') print(f'Century: {faker.century()}') print(f'Year: {faker.year()}') print(f'Month: {faker.month()}') print(f'Month name: {faker.month_name()}') print(f'Day of week: {faker.day_of_week()}') print(f'Day of month: {faker.day_of_month()}') print(f'Time zone: {faker.timezone()}') print(f'AM/PM: {faker.am_pm()}') ``` 第一個示例顯示了偽造的生日,日期時間部分,時區和 AM/PM 方法。 ```py $ date_time.py Date of birth: 2008-08-07 Century: IV Year: 1981 Month: 05 Month name: January Day of week: Saturday Day of month: 26 Time zone: Asia/Oral AM/PM: AM ``` 這是一個示例輸出。 `datetime2.py` ```py #!/usr/bin/env python from faker import Faker faker = Faker() print(f'Datetime this century: {faker.date_time_this_century()}') print(f'Datetime this decade: {faker.date_time_this_decade()}') print(f'Datetime this year: {faker.date_time_this_year()}') print(f'Datetime this month: {faker.date_time_this_month()}') print('-------------------------') print(f'Date this century: {faker.date_this_century()}') print(f'Date this decade: {faker.date_this_decade()}') print(f'Date this year: {faker.date_this_year()}') print(f'Date this month: {faker.date_this_month()}') print('-------------------------') TOTAL_SECONDS = 60*60*24*2 # two days series = faker.time_series(start_date='-12d', end_date='now', precision=TOTAL_SECONDS) for val in series: print(val[0]) ``` 第二個示例顯示了用于生成當前世紀,十年,年份或月份中的日期時間值的方法。 它還包括時間序列值的生成。 ```py $ date_time2.py Datetime this century: 2013-08-06 23:42:47 Datetime this decade: 2010-02-15 01:08:34 Datetime this year: 2019-03-10 05:32:42 Datetime this month: 2019-04-06 10:13:53 ------------------------- Date this century: 2014-11-04 Date this decade: 2018-03-07 Date this year: 2019-02-07 Date this month: 2019-04-04 ------------------------- 2019-03-26 16:16:49 2019-03-28 16:16:49 2019-03-30 16:16:49 2019-04-01 16:16:49 2019-04-03 16:16:49 2019-04-05 16:16:49 ``` 這是一個示例輸出。 `datetime3.py` ```py #!/usr/bin/env python from faker import Faker faker = Faker() print(f'Unix time: {faker.unix_time()}') print(f'Datetime: {faker.date_time()}') print(f'iso8601: {faker.iso8601()}') print(f'Date: {faker.date()}') print(f'Time: {faker.time()}') print('-------------------------') print(f"Datetime between: {faker.date_time_between(start_date='-15y', end_date='now')}") print(f"Date between: {faker.date_between()}") print('-------------------------') print(f"Future datetime: {faker.future_datetime()}") print(f"Future date: {faker.future_date()}") print(f"Past datetime: {faker.past_datetime()}") print(f"Past date: {faker.past_date()}") ``` 第三個示例顯示了用于各種日期時間格式,獲取選定范圍的日期時間值以及生成未來或過去值的方法。 ```py $ date_time3.py Unix time: 1389138339 Datetime: 1988-01-24 09:16:09 iso8601: 2014-04-22T04:19:18 Date: 2009-05-01 Time: 12:07:51 ------------------------- Datetime between: 2011-03-18 21:00:23 Date between: 1992-07-09 ------------------------- Future datetime: 2019-04-11 13:36:02 Future date: 2019-04-14 Past datetime: 2019-03-21 12:27:20 Past date: 2019-03-22 ``` 這是一個示例輸出。 ## 用 Faker 生成 XML 數據 在以下示例中,我們使用 Faker 和 Jinja2 模板生成 XML 數據。 XML 文件將包含用戶。 ```py $ pip install jinja2 ``` 我們安裝 Jinja2 模板引擎。 `fake_xml.py` ```py #!/usr/bin/env python3 from jinja2 import Environment, FileSystemLoader from faker import Faker class User: def __init__(self, first_name, last_name, occupation): self.first_name = first_name self.last_name = last_name self.occupation = occupation faker = Faker() users = [] for _ in range(10): first_name = faker.first_name() last_name = faker.last_name() occupation = faker.job() user = User(first_name, last_name, occupation) users.append(user) file_loader = FileSystemLoader('templates') env = Environment(loader=file_loader) template = env.get_template('users.xml.j2') output = template.render(users=users) print(output, file=open('users.xml', 'w')) ``` 該程序將生成十個用戶的列表。 該列表將傳遞到 Jinja2 模板進行處理。 模板位于`templates`目錄中。 生成的內容被寫入`users.xml`文件。 `templates/users.xml.j2` ```py <?xml version="1.0" encoding="UTF-8"?> <users> {% for user in users %} <user id="{{ loop.index }}"> <firstname>{{ user.first_name }}</firstname> <lastname>{{ user.last_name }}</lastname> <occupation>{{ user.occupation }}</occupation> </user> {% endfor %} </users> ``` 在模板中,我們使用`for`指令來處理用戶列表。 在本教程中,我們使用 Python Faker 在 Python 中生成偽數據。 您可能也對以下相關教程感興趣: [Python Jinja 教程](/python/jinja/)和 [Python 教程](/lang/python/),或列出[所有 Python 教程](/all/#python)。 {% endraw %}
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看