python每日之格式化

python每日之格式化

string=“hello”
#%s打印时结果是hello
print “string=%s” % string # output: string=hello

#%2s意思是字符串长度为2,当原字符串的长度超过2时,按原长度打印,所以%2s的打印结果还是hello
print “string=%2s” % string # output: string=hello

#%7s意思是字符串长度为7,当原字符串的长度小于7时,在原字符串左侧补空格,
#所以%7s的打印结果是 hello
print “string=%7s” % string # output: string= hello

#%-7s意思是字符串长度为7,当原字符串的长度小于7时,在原字符串右侧补空格,
#所以%-7s的打印结果是 hello
print “string=%-7s!” % string # output: string=hello !

#%.2s意思是截取字符串的前2个字符,所以%.2s的打印结果是he
print “string=%.2s” % string # output: string=he

#%.7s意思是截取字符串的前7个字符,当原字符串长度小于7时,即是字符串本身,
#所以%.7s的打印结果是hello
print “string=%.7s” % string # output: string=hello

#%a.bs这种格式是上面两种格式的综合,首先根据小数点后面的数b截取字符串,
#当截取的字符串长度小于a时,还需要在其左侧补空格
print “string=%7.2s” % string # output: string= he
print “string=%2.7s” % string # output: string=hello
print “string=%10.7s” % string # output: string= hello

#还可以用%.s来表示精度,两个的值分别在后面小括号的前两位数值指定
print "string=%
.*s" % (7,2,string) # output: string= he
%d
1num=14

#%d打印时结果是14
print “num=%d” % num # output: num=14

#%1d意思是打印结果为1位整数,当整数的位数超过1位时,按整数原值打印,所以%1d的打印结果还是14
print “num=%1d” % num # output: num=14

#%3d意思是打印结果为3位整数,当整数的位数不够3位时,在整数左侧补空格,所以%3d的打印结果是 14
print “num=%3d” % num # output: num= 14

#%-3d意思是打印结果为3位整数,当整数的位数不够3位时,在整数右侧补空格,所以%3d的打印结果是14_
print “num=%-3d” % num # output: num=14_

#%05d意思是打印结果为5位整数,当整数的位数不够5位时,在整数左侧补0,所以%05d的打印结果是00014
print “num=%05d” % num # output: num=00014

#%.3d小数点后面的3意思是打印结果为3位整数,
#当整数的位数不够3位时,在整数左侧补0,所以%.3d的打印结果是014
print “num=%.3d” % num # output: num=014

#%.0003d小数点后面的0003和3一样,都表示3,意思是打印结果为3位整数,
#当整数的位数不够3位时,在整数左侧补0,所以%.3d的打印结果还是014
print “num=%.0003d” % num # output: num=014

#%5.3d是两种补齐方式的综合,当整数的位数不够3时,先在左侧补0,还是不够5位时,再在左侧补空格,
#规则就是补0优先,最终的长度选数值较大的那个,所以%5.3d的打印结果还是 014
print “num=%5.3d” % num # output: num= 014

#%05.3d是两种补齐方式的综合,当整数的位数不够3时,先在左侧补0,还是不够5位时,
#由于是05,再在左侧补0,最终的长度选数值较大的那个,所以%05.3d的打印结果还是00014
print “num=%05.3d” % num # output: num=00014

#还可以用%.d来表示精度,两个的值分别在后面小括号的前两位数值指定
#如下,不过这种方式04就失去补0的功能,只能补空格,只有小数点后面的3才能补0
print "num=%
.*d" % (04,3,num) # output: num= 014
复制代码
%f
复制代码
import math

#%a.bf,a表示浮点数的打印长度,b表示浮点数小数点后面的精度

#只是%f时表示原值,默认是小数点后5位数
print “PI=%f” % math.pi # output: PI=3.141593

#只是%9f时,表示打印长度9位数,小数点也占一位,不够左侧补空格
print “PI=%9f” % math.pi # output: PI=_3.141593

#只有.没有后面的数字时,表示去掉小数输出整数,03表示不够3位数左侧补0
print “PI=%03.f” % math.pi # output: PI=003

#%6.3f表示小数点后面精确到3位,总长度6位数,包括小数点,不够左侧补空格
print “PI=%6.3f” % math.pi # output: PI=_3.142

#%-6.3f表示小数点后面精确到3位,总长度6位数,包括小数点,不够右侧补空格
print “PI=%-6.3f” % math.pi # output: PI=3.142_

还可以用%.f来表示精度,两个的值分别在后面小括号的前两位数值指定
如下,不过这种方式06就失去补0的功能,只能补空格
print "PI=%
.*f" % (06,3,math.pi) # output: PI=_3.142

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用Python进行全球疫情数据可视化分析的代码实现。在这里,我们使用了pandas、matplotlib和Plotly库。 首先,我们需要获取全球疫情数据。在这里,我们使用了Johns Hopkins大学提供的COVID-19仪表板数据。可以通过下面的代码从GitHub上获取数据: ```python import pandas as pd url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/' \ 'csse_covid_19_data/csse_covid_19_time_series/' \ 'time_series_covid19_confirmed_global.csv' df_confirmed = pd.read_csv(url) ``` 接下来,我们可以对数据进行一些处理,以便进行可视化分析。在这里,我们将数据按国家/地区分组,并计算每个国家/地区的总病例数: ```python df_confirmed = df_confirmed.groupby('Country/Region').sum().reset_index() ``` 接下来,我们可以使用matplotlib来绘制全球疫情的趋势图,以显示每日新增病例数和死亡人数的变化。以下是绘制每日新增病例数的代码: ```python import matplotlib.pyplot as plt dates = df_confirmed.columns[4:] total_confirmed = df_confirmed[dates].sum(axis=0) new_cases = [] for i in range(len(total_confirmed)): if i == 0: new_cases.append(total_confirmed[i]) else: new_cases.append(total_confirmed[i] - total_confirmed[i-1]) fig, ax = plt.subplots(figsize=(12, 8)) ax.bar(dates, new_cases) ax.set_xlabel('Date') ax.set_ylabel('New cases') ax.set_title('Global daily new confirmed cases') plt.xticks(rotation=90) plt.show() ``` 接下来,我们可以使用Plotly来创建交互式图表。以下是使用Plotly创建热力图的代码: ```python import plotly.express as px df_confirmed_long = pd.melt(df_confirmed, id_vars=['Country/Region'], value_vars=dates, var_name='Date', value_name='Confirmed') df_confirmed_long['Date'] = pd.to_datetime(df_confirmed_long['Date']) fig = px.choropleth(df_confirmed_long, locations='Country/Region', locationmode='country names', color='Confirmed', hover_name='Country/Region', animation_frame='Date', projection='natural earth', range_color=[0, df_confirmed_long['Confirmed'].max()], title='Global confirmed cases') fig.show() ``` 这段代码将数据转换为长格式,然后使用Plotly的choropleth函数创建热力图。我们还可以使用其他Plotly函数和参数来创建不同类型的图表。 以上是使用Python进行全球疫情数据可视化分析的简单示例代码,您可以根据自己的需求和喜好进行进一步的修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值