# 字符串替換, 把特定字符替換成空字符 import re newString = re.sub('[ABCD ]','',data) # 字符串分割,使用逗號分割 newString = data.split(',') # 字符串中字符轉數字 newArray = [float(x) for x in string]
https://www.cnblogs.com/zhouzhiyao/p/11498907.html
字符串替換
https://www.cnblogs.com/2bjiujiu/p/7257744.html
批量讀取字符串文件
https://www.cnblogs.com/Jaguar/p/10688427.html
1 打開日志文件
雖然,日志文件的后綴為.log,但是基本上與文本文件沒有區別,按照一般讀取文本文件的方式打開即可:
fp =open("e:\\data.log") fp.close()
fp =open("e:\\data.log") for line in fp.readlines(): # 遍歷每一行 filename = line[:14] # 每行取前14個字母,作為下面新建文件的名稱 content = line[14:] # 每行取第15個字符后的所有字符,作為新建文件的內容 with open("e:\\"+filename+".txt","w") as fp2: fp2.write(content+"\n") fp.close()
2 提取目標信息
日志文件每行字符串由空格分隔,例如對第1個字段(IP、時間等)感興趣,則使用split()方法對每行字符串進行切片,將第1個子字符串存到列表里,用於下一步處理。
示例代碼:
#!/usr/bin/python # -*- coding: UTF-8 -*- txt = "Google#Runoob#Taobao#Facebook" # 第二個參數為 1,返回兩個參數列表 x = txt.split("#", 1) print x