Process each line of text file using Python
<p>I am fairly new to Python. I have a text file containing many blocks of data in following format along with other unnecessary blocks.</p>

<pre><code>    NOT REQUIRED :: 123
    Connected Part-1:: A ~$
    Connected Part-3:: B ~$         
    Connector Location:: 100 200 300 ~$
    NOT REQUIRED :: 456
    Connected Part-2:: C ~$
</code></pre>

<p>i wish to extract the info (A,B,C, 100 200 300) corresponding to each property ( connected part-1, Connector location) and store it as list to use it later. I have prepared following code which reads file, cleans the line and store it as list.</p>

<pre><code>    import fileinput
    with open('C:/Users/file.txt') as f:
    content = f.readlines()
    for line in content:
            if 'Connected Part-1' in line or 'Connected Part-3' in line:
                    if 'Connected Part-1' in line:
                            connected_part_1 = [s.strip(' \n ~ $ Connected Part -1 ::') for s in content]                               
                            print ('PART_1:',connected_part_1)                                  
                    if 'Connected Part-3' in line:
                            connected_part_3 = [s.strip(' \n ~ $ Connected Part -3 ::') for s in content]                               
                            print ('PART_3:',connected_part_3)                              
            if 'Connector Location' in line:                        
                    # removing unwanted characters and converting into the list
                    content_clean_1 = [s.strip('\n ~ $ Connector Location::') for s in content] 
                    #converting a single string item in list to a string
                    s = " ".join(content_clean_1)
                    # splitting the string and converting into a list
                    weld_location= s.split(" ")
                    print ('POSITION',weld_location)    
</code></pre>

<p>here is the output</p>

<pre><code>   PART_1: ['A', '\t\tConnector Location:: 100.00 200.00 300.00', '\t\tConnected Part-3:: C~\t']
   POSITION ['d', 'Part-1::', 'A', '\t\tConnector', 'Location::', '100.00', '200.00', '300.00', '\t\tConnected', 'Part-3::', 'C~\t']
   PART_3: ['1:: A', '\t\tConnector Location:: 100.00 200.00 300.00', '\t\tConnected Part-3:: C~\t']
</code></pre>

<p>From the output of this program, i may conclude that, since 'content' is the string consisting all the characters in the file, the program is not reading an individual line. Instead it is considering all text as single string. Could anyone please help in this case?</p>

<p>I am expecting following output:</p>

<pre><code>   PART_1: ['A']
   PART_3: ['C']
   POSITION: ['100.00', '200.00','300.00']
</code></pre>

<p>(Note) When i am using individual files containing single line of data, it works fine. Sorry for such a long question</p>

<p>I will try to make it clear, and show how I would do it without <code>regex</code>. First of all, the biggest issue with the code presented is that when using the <code>string.strip</code> function the entire content list is being read:</p>

<pre><code>connected_part_1 = [s.strip(' \n ~ $ Connected Part -1 ::') for s in content]
</code></pre>

<p>Content is the entire file lines, I think you want simply something like:</p>

<pre><code>connected_part_1 = [line.strip(' \n ~ $ Connected Part -1 ::')]
</code></pre>

<p>How to parse the file is a bit subjective, but given the file format posted as input, I would do it like this:</p>

<pre><code>templatestr = "{}: {}"

with open('inputreadlines.txt') as f:
    content = f.readlines()
    for line in content:
        label, value = line.split('::')
        ltokens = label.split()
        if ltokens[0] == 'Connected':
            print(templatestr.format(
                ltokens[-1], #The last word on the label
                value.split()[:-1])) #the split value without the last word '~$'
        elif ltokens[0] == 'Connector':
            print(value.split()[:-1]) #the split value without the last word '~$'
        else: #NOT REQUIRED
            pass
</code></pre>

<p>You can use the <code>string.strip</code> function to remove the funny characters '~$' instead of removing the last token as in the example.</p>

python python-3.x