整体思路
1. 去空去重 去掉不想要的字符串
2. 拆成两列一维数组
3. 合并成一 列二维数组
4. 二维数组转csv
```python
# -*- coding: utf-8 -*-
import os
import xlwt
a = os.getcwd() #获取当前目录
print (a) #打印当前目录
os.chdir('D:\Work\Tool\Log-data\RLog') #定位到新的目录,请根据你自己文件的位置做相应的修改
a = os.getcwd() #获取定位之后的目录
print(a) #打印定位之后的目录
#读取目标txt文件里的内容,并且打印出来显示
with open('LogText.txt','r') as raw:
for line in raw:
print (line)
# 去掉txt里面的空白行,除特定格式外的字符串 并保存到新的文件中
with open('LogText.txt', 'r', encoding='utf-8') as fr, open('output.txt', 'w', encoding='utf-8') as fd:
for text in fr.readlines():
if text.split() and "channel: " in text:
fd.write(text)
with open('output.txt','r') as raw:
for line in raw:
print (line)
print('success')
#创建一个workbook对象,相当于创建一个Excel文件
book = xlwt.Workbook(encoding='utf-8',style_compression=0)
#wb = openpyxl.Workbook(encoding='utf-8',style_compression=0) # 打开一个将写的文件
#ws=wb.active
# 创建一个sheet对象,一个sheet对象对应Excel文件中的一张表格。
sheet = book.add_sheet('Output', cell_overwrite_ok=True)
header=['channel','value']
header1=['channel','value']
#对文本内容进行多次切片得到想要的部分
n=1
# 3:遍历列表,将每一行的数据写入csv
# 最后,将以上操作保存到指定的Excel文件中
a1=[]
b1=[]
c=[]
d=[]
e=[]
with open('output.txt', 'r+') as fd:
for text in fd.readlines():
x = text.split(':')[1]
y = text.split(':')[2]
a=x.split('value')[0]
b=y.split('\n')[0]
#b=text.split(':')[2]
# print (x.split('value'))
# print (y.split('\n'))
d=a+b
a1.append(a)
b1.append(b)
#d.append(d)
f=x.split('value')[0]
g=y.split('\n')[0]
d=x.split('value')[0]+','+y.split('\n')[0]
new_d=d.split(",");
# a1.append(a)
# b1.append(b)
#e=[list(t) for t in zip(f,g)]
e.append(new_d)#[[],[],[]]
#
# csvs = "".join("(" + x.split('value')[0] + "," + y.split('\n')[0] + ")," + '\n')
for i in range( max ( len( a1 ), len( b1 ) ) ):
if a1:
c.append( a1.pop() )
if b1:
c.append( b1.pop() )#[,,,,,,,,,]
"""
Yongqiang Cheng
"""
polylines_list = c
interval = 2
polyline_set = []
if (len(polylines_list) % 2 == 0):
for idx in range(0, len(polylines_list), 2):
polyline_set.append([polylines_list[idx], polylines_list[idx + 1]])
else:
print("IndexError: list index out of range.")
print(polyline_set)
# coding:utf-8
import csv
f = open('222.csv','w',newline='')
writer = csv.writer(f)
writer.writerow(header)
for i in polyline_set:
writer.writerow(i)
f.close()
```