0

Need it to only read the integer values, and not the strings.

This is an example of a line in the text file:

yye5     mooProject   No     yeetcity   Nrn de   0      .1       .5       0

We want to skip the first 5 columns (Nrn de is one column) and put every line in the file (which looks like this) into a numpy or pandas array.

DanielM
  • 3,598
  • 5
  • 37
  • 53
  • 1
    Possible duplicate: https://stackoverflow.com/questions/3501382/checking-whether-a-variable-is-an-integer-or-not – Hoog Jul 09 '19 at 14:33
  • Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Akaisteph7 Jul 09 '19 at 14:37

1 Answers1

0

Try/Except block is your friend.

x =  ('yye5','mooProject','No','yeetcity','Nrn','de','0','.1','.5','0')
result = []
for i in x:
    try:
        result.append(float(i))
    except ValueError:
        pass

print(result)
eatmeimadanish
  • 3,809
  • 1
  • 14
  • 20
  • Thank you sir, but this did not work as it did not append any values to result, so an error must have been produced every time. – Sir Cappery Jul 09 '19 at 15:50
  • Thank you, now I tested it and it works. But for some reason everything comes out in 1 line. 0 /n .1 /n .5 /n 0 – Sir Cappery Jul 09 '19 at 20:19
  • I am not sure how that is possible. It is printing a list. You must be using some quirky editor. Line breaks would mean that it is converting the list into a string result. That does not happen in the code above. – eatmeimadanish Jul 09 '19 at 20:48