python 如何输出包含特殊字符串的行的前五位字符到指定文件 python中找到特定的字符并输出到另一个文件中

python\u6309\u7167\u6307\u5b9a\u5b57\u7b26\u4e32\u6253\u5370\u51fa\u8fd9\u4e2a\u5b57\u7b26\u4e32\u7684\u540e\u9762\u51e0\u884c\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u4e32\u5f00\u5934\u7684\u884c\u3002

Python Code:
from configparser import ConfigParsercfgParser = ConfigParser()cfgParser.read(r"D:\a.txt") # \u8def\u5f84\u4fee\u6539\u4e3a\u4f60\u7535\u8111\u4e2d\u7684\u5373\u53efcfgParser.get("test", "path")\u8f93\u51fa\uff1a'/test1/test'

import refname = 'a.txt'with open(fname) as f: s = f.read()mwp = re.findall(r'([0-9.e+]+)\s*#\s*MWP\b', s)width = re.findall(r'\s34\s+([0-9.e+]+)', s)

可以用如下代码试一下:

import re

import xlwt

text = '''2.7% 6944/com.imrn: 2% user + 0.7% kernel / faults: 344 minor
0% 6966/com.imrn:core: 0% user + 0% kernel / faults: 74 minor
43% 6944/com.imrn: 2% user + 0.7% kernel / faults: 344 minor'''

book = xlwt.Workbook(encoding='utf-8', style_compression=0)
sheet = book.add_sheet('mysheet', cell_overwrite_ok=True)
text = re.findall(r".+?(?=344 minor)", text)
x = 0
for s in text:
    s = re.findall(r".+(?=6944)", s)
    print(s)
    sheet.write(x, 0, s)
    x = x + 1
book.save('test.xls')

结果如下:



扩展阅读:python初学编程必背 ... python代码自动生成器 ... c++和python先学哪个 ... python编程入门自学 ... python符号含义大全 ... python 字符串 包含 ... python基础代码大全 ... python三种格式化输出 ... python 1-100包含7的数 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网