﻿ python中的format函数（python中format用法详解）38-慕源网

# python中的format函数（python中format用法详解）38 ### python中的format函数

python中format函数用于字符串的格式化

`Pandas.format(value)`

value表示任何字符串或浮点值等。

### python中format用法详解

#### 示例#1

``````import pandas as pd
info = {'Month' : ['September', 'October', 'November', 'December'],
'Salary': [ 3456789, 987654, 1357910, 90807065]}
df = pd.DataFrame(info, columns = ['Month', 'Salary'])
print("Existing Dataframe is :\n", df)
pd.options.display.float_format = '{:.3f}'.format
print('\nFinal :\n', df)`````` #### 示例#2

``````import pandas as pd
info = {'Month' : ['September', 'October', 'November', 'December'],
'Salary': [ 3456789.12, 987654.34, 1357910.56, 90807065.78]}
df = pd.DataFrame(info, columns = ['Month', 'Salary'])
print("Existing Dataframe is :\n", df)
pd.options.display.float_format = '{:.3f}'.format
print('\nFinal :\n', df)`````` String format() 是 Python3 中的一种字符串排列策略，可以进行多次替换，值得整理。这种策略让我们可以通过位置设计将字符串内部的组件连接起来。

### 结论

format()功能更强大，该函数把字符串当成一个模板，通过传入的参数进行格式化，并且使用大括号‘{}’作为特殊字符代替
{"ft_lang_label":"__label__zh","ft_lang_prob":0.6912299,"math_prob":0.9542304,"size":1308,"snap":"2022-27-2022-33","text_gpt3_token_len":661,"char_repetition_ratio":0.11886503,"word_repetition_ratio":0.5090909,"special_character_ratio":0.29663607,"punctuation_ratio":0.23671497,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95090204,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-07T17:11:12Z\",\"WARC-Record-ID\":\"<urn:uuid:0b829dba-ec65-4189-a94d-1605ae4cc4d4>\",\"Content-Length\":\"64737\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ac79b9a8-d4ec-4280-b648-cd11978a6b01>\",\"WARC-Concurrent-To\":\"<urn:uuid:15aeb04f-733e-4012-883e-e8bf521975d6>\",\"WARC-IP-Address\":\"47.92.247.119\",\"WARC-Target-URI\":\"https://www.muyuanzhan.com/tutorials/python/6149.html\",\"WARC-Payload-Digest\":\"sha1:UCOX5HWBPAFJNMTBH6AFC2GQNXQMIY5F\",\"WARC-Block-Digest\":\"sha1:L6CAIJY2VKPNZUGPNIEGN3TL26IPTDDB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104495692.77_warc_CC-MAIN-20220707154329-20220707184329-00649.warc.gz\"}"}