focus-z
# 字符串替换, 把特定字符替换成空字符
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

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-03
  • 2022-12-23
  • 2021-10-03
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案