意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

Python编程模块常用的包括什么,怎样实现

来源:恒创科技 编辑:恒创科技编辑部
2023-12-26 00:49:59
这篇文章给大家分享的是“Python编程模块常用的包括什么,怎样实现”,对大家学习和理解有一定的参考价值和帮助,有这方面学习需要的朋友,接下来就跟随小编一起学习一下吧。

文件流的读写

读取保存数据为数组的txt文件

使用try进行异常发现,使用while检测文件末尾进行读取


Python编程模块常用的包括什么,怎样实现

file_to_read = raw_input("Enter file name of tests (empty string to end program):")
try:
    infile = open(file_to_read, 'r')
    while file_to_read != " ":
        file_to_write = raw_input("Enter output file name (.csv will be appended to it):")
        file_to_write = file_to_write + ".csv"
        outfile = open(file_to_write, "w")
        readings = (infile.readline())
        print readings
        while readings != 0:
            global count
            readings = int(readings)
            minimum = (infile.readline())
            maximum = (infile.readline())

使用for遍历读取的每一行,进行一次性的读取和输入

下面调用的程序读取的数据是

result = list()
    with open('../test/parameter.txt') as  f:
        for line in f.readlines():
            temp = list()
            # 逐个遍历对应每一行元素,将之转为对应的数据
            b = line.strip(",][").split(',')
            if(len(b) >= 5):
                b.pop()
            for a in b:
                a = a.replace('[','').replace(']','')
                temp.append(float(a))
            result.append(temp)
            #print("中途打印的temp是",temp)
            #print("加入到result中的结果是",result)

删除str中的特定字符

删除字符串首尾的多余字符串strip()

# 删除字符串中多余字符
def string_remove():
   str1 = ' abc     \n'
   print str1.strip()   # abc

   str2 = '----abcdf++++'
   print str2.strip('-+')  # abcdf

replace函数,删除字符串中某一个所有的字符串

ss = 'old old string'
ret = ss.replace('old', 'new', 1)
print(ret)

sub函数,同时删除多个字符串,这里使用了正则表达式

str2 = '\nabc\nwrt22\t666\t'  # 删除字符串中的所有\n,\t
import re
print(re.sub('[\n\t]','',str2))   # abcwrt22666

感谢各位的阅读,以上就是“Python编程模块常用的包括什么,怎样实现”的内容了,经过本文的学习后,相信大家对Python编程模块常用的包括什么,怎样实现都有更深刻的体会了吧。这里是恒创科技,小编将为大家推送更多相关知识点的文章,欢迎关注!
上一篇: CSRF防跨站攻击是怎么回事,如何解决应对 下一篇: 如何用Python实现表情包生成文字,思路及代码是什么