濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > 聊一聊python常用的編程模塊

聊一聊python常用的編程模塊

熱門標(biāo)簽:騰訊外呼線路 廣告地圖標(biāo)注app 陜西金融外呼系統(tǒng) 公司電話機(jī)器人 激戰(zhàn)2地圖標(biāo)注 白銀外呼系統(tǒng) 哈爾濱ai外呼系統(tǒng)定制 唐山智能外呼系統(tǒng)一般多少錢 海南400電話如何申請(qǐng)

文件流的讀寫

讀取保存數(shù)據(jù)為數(shù)組的txt文件

使用try進(jìn)行異常發(fā)現(xiàn),使用while檢測(cè)文件末尾進(jìn)行讀取

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遍歷讀取的每一行,進(jìn)行一次性的讀取和輸入

下面調(diào)用的程序讀取的數(shù)據(jù)是

result = list()
    with open('../test/parameter.txt') as  f:
        for line in f.readlines():
            temp = list()
            # 逐個(gè)遍歷對(duì)應(yīng)每一行元素,將之轉(zhuǎn)為對(duì)應(yīng)的數(shù)據(jù)
            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中的結(jié)果是",result)

刪除str中的特定字符

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

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

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

replace函數(shù),刪除字符串中某一個(gè)所有的字符串

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

sub函數(shù),同時(shí)刪除多個(gè)字符串,這里使用了正則表達(dá)式

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

以上就是聊一聊python常用的編程模塊的詳細(xì)內(nèi)容,更多關(guān)于python編程模塊的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • python—sys模塊之獲取參數(shù)的操作
  • Python協(xié)程asyncio模塊的演變及高級(jí)用法
  • python process模塊的使用簡(jiǎn)介
  • python文件目錄操作之os模塊
  • Python collections模塊的使用技巧
  • Python使用random模塊實(shí)現(xiàn)擲骰子游戲的示例代碼
  • Python爬蟲基礎(chǔ)之requestes模塊
  • Python多線程編程之threading模塊詳解
  • python通配符之glob模塊的使用詳解
  • Python基礎(chǔ)之模塊相關(guān)知識(shí)總結(jié)

標(biāo)簽:黔西 常德 黑龍江 惠州 益陽 鷹潭 上海 四川

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《聊一聊python常用的編程模塊》,本文關(guān)鍵詞  聊,一聊,python,常用的,常,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《聊一聊python常用的編程模塊》相關(guān)的同類信息!
  • 本頁收集關(guān)于聊一聊python常用的編程模塊的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    邵阳县| 延安市| 丽江市| 乾安县| 莱阳市| 绥阳县| 绥宁县| 岱山县| 安平县| 崇信县| 喀什市| 韩城市| 珠海市| 宣城市| 福海县| 林州市| 保山市| 博乐市| 尉氏县| 绵阳市| 手游| 普陀区| 即墨市| 万安县| 常德市| 徐闻县| 沂源县| 日照市| 永和县| 新乡县| 焉耆| 长葛市| 资溪县| 浑源县| 陇南市| 保靖县| 木兰县| 长顺县| 山东| 泽普县| 新平|