Python Forum
How to access each line in for loop and split string
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to access each line in for loop and split string
#1
I want to read text file and write csv: 
I use the following code,
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. I want to split the string in each line, and save first column into para_list, column3 into end_Value
2. in end_values, I want to keep the first string (if number, or logical operator), or whole siring if it is text.

My desired output:
Output:
    para_list:     parq     10Lqr     29Hyt     Zgeat1     Beget          end_values:     33.0 mm      23.0     1.0     Noraml set     12
please any one help, many thanks in advance,
my text file is below:    
Output:
    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     -------------------------------------
Reply
#2
The split method will split those lines into three parts. Then you can abstract those parts with list comprehensions:

reqCols = [line.split() for line in reqData]
para_list = [line[0] for line in reqCols]
end_values = [line[2] for line in reqCols]
That will give you two lists with the lines you want to output.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Great, that is what i was looking for!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split string into 160-character chunks while adding text to each part iambobbiekings 9 9,583 Jan-27-2021, 08:15 AM
Last Post: iambobbiekings
  Reading a text until matched string and print it as a single line cananb 1 2,017 Nov-29-2020, 01:38 PM
Last Post: DPaul
  Adding string numbers, while loop and exit without input. Jose 11 7,477 Apr-15-2020, 08:34 AM
Last Post: Jose
  Split String lekuru01 6 3,474 Mar-19-2019, 10:42 AM
Last Post: lekuru01
  [split] Splitting a string into six pieces susmis666 1 2,207 Dec-25-2018, 06:16 PM
Last Post: ichabod801
  ValueError-trying to split a line samu3l 7 5,413 Feb-06-2018, 07:28 PM
Last Post: Larz60+
  split the line fixed length tokens bb8 5 5,634 Nov-25-2017, 06:18 PM
Last Post: heiner55
  Reverse string sentence with for loop? BillGates 3 6,079 May-02-2017, 04:15 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020