如何访问for循环和拆分字符串

时间:2017-08-10 11:08:24

标签: python-2.7 machine-learning

我想阅读文本文件并编写csv: 我使用以下代码,

import glob
import os
import re
list_of_files=glob.glob('D:\Mekala_Backupdata\PythonCodes/textFile.txt')
for fileName in list_of_files:
    fin=open(fileName,"r")
    data_list=fin.readlines()
    fin.close()
    targetSubString='-------'
    indices=[idx for idx,s in enumerate(data_list) if targetSubString in s]
    reqData=data_list[indices[0]+1:indices[1]]
    fout=open("stripD.csv","w")
    fout.writelines(reqData)
    fout.flush()
    fout.close
  1. 我想在每一行中拆分字符串,并将第一列保存到para_list,将column3保存到end_Value
  2. 在end_values中,我想保留第一个字符串(如果是数字,或逻辑运算符),或者如果是文本则保留整个字符串。
  3. 我想要的输出:

    para_list:
    parq
    10Lqr
    29Hyt
    Zgeat1
    Beget
    
    end_values:
    33.0 mm 
    23.0
    1.0
    Noraml set
    12
    

    请任何人帮助,非常感谢,

    我的文字文件如下:

    File Name:thUIK003K
    
    Version:002BA07Gh
    Name:HUJKO
    
    
    Parameter        Start        End
    -------------------------------------
    parq             56 mm        33.0 mm 
    10Lqr            12.0 mm      23.0 mm
    29Hyt            0.0 %        1.0 %
    Zgeat1           normal set   noraml set
    Beget            12 km        12 km
    -------------------------------------
    other events:
    11000 vent trig
    213455 alram
    
    xpara  ypara
    1      3
    2      3
    4      8
    6      10
    -------------------------------------
    

1 个答案:

答案 0 :(得分:1)

My environment is python3.5 and wiondows10.
import glob
import os
import re

list_of_files=glob.glob('E:/StackOverFlow/python/01/pythonCodes/*.txt')

for fileName in list_of_files:
    fin = open(fileName, "r")
    data_list = fin.readlines()
    fin.close()
    targetSubstring='-------'

    count = 0
    data_list2=[]
    line = data_list[0] 
    for line in data_list:
        if targetSubstring in line and count > 0 :
            break;

        if targetSubstring in line  or count > 0 :
            count=count+1
            data_list2.append(line)

    data_list2.pop(0)

    columns = []
    column1 = []
    column3 = []

    for line in data_list2 :
        columns = re.split('\s{2,}',line)
        column1.append(columns[0])
        column3.append(columns[2])

    #fout = open('stripD.csv',"w") 
    path,ext = os.path.splitext(os.path.basename(fileName))
    fout = open(path +'_' + 'stripD.csv',"w")

    fout.write('para_list:\n')
    for line in column1 :
        fout.write(line)
        fout.write('\n')

    fout.write('end_values:\n')

    for line in column3 :
        line_tmp = re.findall('[\d.]{2,}',line)
        print(line_tmp)
        if len(line_tmp) !=0 :
            fout.write(line_tmp[0].strip())
        else:
            fout.write(line.strip())

        fout.write('\n')

    fout.close()
input:#same contents
textFile.txt
textFile01.txt

result is below

output file name:textFile_stripD.csv
para_list:
parq
10Lqr
29Hyt
Zgeat1
Beget
end_values:
33.0
23.0
1.0
noraml set
12

output file name:textFile01_stripD.csv
para_list:
parq
10Lqr
29Hyt
Zgeat1
Beget
end_values:
33.0
23.0
1.0
noraml set
12