如何在python中的类别中格式化我的数据

Python学习者

一个python程序来读取rainfall.txt文件,然后写出一个名为rainfallfmt.txt的新文件。数据应按年度总降水量分为以下几类:[60-70],[71-80],[81-90],[91-]。在每个类别下,新文件应设置每行的格式,以便在25个字符宽的字段中右对齐城市,并在5个字符宽的字段中打印降雨数据,并在字段右侧添加1位数字小数点。到目前为止,这就是我所拥有的。问题是我不知道如何分类?您能帮我一下,让我知道如何解决这个问题吗?

================================================

 # read the rainfall.txt then write out a new file called rainfall.txt
    # the data should be grouped on the total annual rainfall field into the 
    # categories: [60-70], [71-80], [81,90], [91-]

        import os.path

        def main():
            endofprogram = False
            try:
                InputFileName = input('Enter name of input file: ')
                infile = open(InputFileName,'r')
                OutputFileName = input('Enter name of output file: ')
                # determine wether name exists
                while True:
                    if os.path.isfile(OutputFileName):
                        OutputFileName = input('File Exists. Enter name again: ')
                    else:
                        outfile = open(OutputFileName,'w')
                        break
            except IOError:
                print("Error opening file - End of program")
                endofprogram = True

                #If there is not exception, start reading the input file
                #Write the same data in formated form in new file.
                if endofprogram == False:
                    data = []
                    for line in infile:
                    .
                    .# I dont know what to do in here!
                    .
                    outfile.write(data[0])
        main()
Python学习者
# read the rainfall.txt then write out a new file called rainfall.txt
# the data should be grouped on the total annual rainfall field into the 
# categories: [60-70], [71-80], [81,90], [91-]
import os.path

def main():

    endofprogram = False
    try:
        InputFileName = input('Enter name of input file: ')
        infile = open(InputFileName,'r')
        OutputFileName = input('Enter name of output file: ')
        # determine wether name exists
        while True:
            if os.path.isfile(OutputFileName):
                OutputFileName = input('File Exists. Enter name again: ')
            else:
                outfile = open(OutputFileName,'w')
                break
    except IOError:
        print("Error opening file - End of program")
        endofprogram = True

        #If there is not exception, start reading the input file
        #Write the same data in formated form in new file.
    if endofprogram == False:
        cat_60 = []
        cat_71 = []
        cat_81 = []
        cat_91 = []
        for line in infile:
            city, rain = line.split(' ')
            rain = float(rain)

            if 60 <= rain < 70:            
                cat_60.append((city, rain)) # Storing a tuple in the list
            elif 70 <= rain < 80:
                cat_71.append((city, rain))  
            elif 80 <= rain < 90:
                cat_81.append((city, rain))
            elif 90 <= rain :
                cat_91.append((city, rain))

        outfile.write("[60-70]"+'\n')
        for i in range(len(cat_60)):
            city = cat_60[i][0]
            rain = cat_60[i][1]
            outfile.write('%+25s'%(city)+'%5.1f'%(rain)+'\n')

        outfile.write("[70-80]"+'\n')
        for i in range(len(cat_71)):
            city = cat_71[i][0]
            rain = cat_71[i][1]                
            outfile.write('%+25s'%(city)+'%5.1f'%(rain)+'\n')

        outfile.write("[80-90]"+'\n')
        for i in range(len(cat_81)):
            city = cat_81[i][0]
            rain = cat_81[i][1]
            outfile.write('%+25s'%(city)+'%5.1f'%(rain)+'\n')

        outfile.write("[91-]"+'\n')
        for i in range(len(cat_91)):
            city = cat_91[i][0]
            rain = cat_91[i][1]
            outfile.write('%+25s'%(city)+'%5.1f'%(rain)+'\n')
main()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Python中的单个HTTP POST请求中格式化多个数据?

来自分类Dev

我该如何在angularjs中格式化json?

来自分类Dev

我如何在 groovy 脚本中格式化日期

来自分类Dev

如何在AngularJS的视图中格式化数据?

来自分类Dev

如何在rmarkdown中格式化数据帧

来自分类Dev

如何在ASP.NET中格式化数据

来自分类Dev

如何在PrettyTable中格式化数据?

来自分类Dev

你如何在python中格式化日期时间

来自分类Dev

如何在python中格式化电话号码

来自分类Dev

如何在python中格式化列表输出

来自分类Dev

如何在python中格式化git show的输出

来自分类Dev

如何在Grape中的参数中格式化DateTime

来自分类Dev

如何在Struts 2中格式化货币?

来自分类Dev

如何在Struts 2中格式化货币?

来自分类Dev

在保存到Mongoose(ExpressJS)中之前如何在Model中格式化数据

来自分类Dev

在python中格式化数据

来自分类Dev

如何从excel中检索数据并在python中格式化它们?

来自分类Dev

如何在Python 2.7中格式化元组列表?

来自分类Dev

如何在数据表的数据列中格式化datetimeshortstring格式

来自分类Dev

如何在 Flutter 中格式化 DateTime,如何在 Flutter 中获取当前时间?

来自分类Dev

Yii2 - 如何在我的 GridView 中格式化页脚?

来自分类Dev

如何在javascript中格式化数据库日期时间?

来自分类Dev

将数据发布到REST端点时如何在AngularJS中格式化日期

来自分类Dev

如何在Angular 2中格式化JS对象中的日期?

来自分类Dev

如何在Django模板中格式化XMLtext

来自分类Dev

如何在Jmeter中格式化JSON响应?

来自分类Dev

如何在C中格式化小数?

来自分类Dev

如何在Java中格式化日期范围?

来自分类Dev

如何在SQL SERVER中格式化日期时间

Related 相关文章

热门标签

归档