2

I have a .txt file, where I want to save only following characters "N", "1.1" ,"XY", "N", "2.3" ,"xz" in an array. The .txt file looks like this:

[   TITLE

    N 1.1 XY
    N 2.3 XZ

]

Here is my code:

src = open("In.txt", "r")

def findOp (row):
    trig = False
    temp = ["", "", ""]
    i = 1
    n = 0
    for char in row:  
        i += 1
        if (char != '\t') & (char != ' ') & (char != '\n'):
            trig = True
            temp[n] += char
        else:
            if trig:
                n += 1
                trig = False

    return temp

for line in src.readlines():
print(findOp(line))

The Output from my code is:

['[', 'TITLE', '']
['', '', '']
['N', '1.1', 'XY']
['N', '2.3', 'XZ']
['', '', '']
[']', '', '']

The problem is the program also saves whitespace characters in an array which i dont want.

Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
naijboy90
  • 39
  • 3

4 Answers4

1

I would recommend the trim()-function with witch one you can remove whitespace from a string

Whitespace on both sides:

s = s.strip()

Whitespace on the right side:

s = s.rstrip()

Whitespace on the left side:

s = s.lstrip()
DoFlamingo
  • 232
  • 1
  • 18
0

Try this :

with open('In.txt', 'r') as f:
    lines = [i.strip() for i in f.readlines() if i.strip()][1:-1]
output = [[word for word in line.split() if word] for line in lines]

Output :

[['N', '1.1', 'XY'], ['N', '2.3', 'XZ']]
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
0

Try numpy.genfromtxt:

import numpy as np
text_arr = np.genfromtxt('In.txt', skip_header = 1, skip_footer = 1, dtype = str)
print(text_arr)

Output:

[['N' '1.1' 'XY']
 ['N' '2.3' 'XZ']]

Or if you want list, add text_arr.tolist()

Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
0

You could check the return array before exiting:

def findOp(row):
    trig = False
    temp = ["", "", ""]
    i = 1
    n = 0
    for char in row:
        i += 1
        if (char != '\t') & (char != ' ') & (char != '\n'):
            trig = True
            temp[n] += char
        else:
            if trig:
                n += 1
                trig = False

    # Will return `temp` if all elements eval to True otherwise
    # it will return None        
    return temp if all(temp) else None

The value None can then be used as a check condition in subsequent constructs:

for line in src.readlines():
    out = findOp(line)
    if out:
        print(out)

>> ['N', '1.1', 'XY']
>> ['N', '2.3', 'XZ']
sal
  • 3,515
  • 1
  • 10
  • 21
  • Can you tell me what "_org" means in "out = findOp_org(line)" – naijboy90 Dec 02 '19 at 08:10
  • If i use your way, the output still gives me the array ['TITLE'] which i dont want to save – naijboy90 Dec 02 '19 at 08:24
  • `findOp_org` : just copy/paste from my test code, fixed now. `TITLE` should not be there, because at that iteration the array `temp` is `['[', 'TITLE', '']` which has a `False` element in it, and would not pass the `all` test. – sal Dec 02 '19 at 16:22
  • Thank you it works. I want now to replace the Characters in the arrays. An example: Replace 'N' with 'Not'. Do you now how to do it whithout using numpy. – naijboy90 Dec 11 '19 at 11:58
  • Yes. You can loop through the list and replace the value, or simply create a new list with the replacement via list comprehension: `new_out = ['Not' if x == 'N' else x for x in out]` . Check this other article: https://stackoverflow.com/questions/1540049/replace-values-in-list-using-python – sal Dec 11 '19 at 16:58