Excluding a certain value from a text file

Hey guys I want some help over here!

I want to extract all values and exclude this value ‘-9999.000000’ from the text file!
another issue is it deals with the txt file as strings!

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

skip_num = '-9999.000000'
skip_num2 = -9999.000000

file_path = "******\\Desktop\\Top1.txt"
with open(file_path, 'r') as file:
    lines = file.readlines()

for line in lines:
    if line == skip_num or line == skip_num2:
        continue
    print(line)

part of the text file for ex:

1691.536209 1692.370721 1693.039722 1693.561744 1693.973645 1694.314977
1694.618101 1694.896711 1695.131911 1695.272466 1695.279960 1695.158461
1694.952690 1694.742281 1694.632870 1694.696035 1694.924255 1695.240304
1695.556093 1695.778526 1695.844049 1695.750505 1695.560313 1695.330033
1695.103891 1694.913418 1694.788435 1694.711884 1694.665856 -9999.000000
-9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000
-9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000
-9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000
-9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000
-9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000
-9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000
-9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000
-9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000
-9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000
-9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000
-9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000 -9999.000000

it is not excluding the values for some reason!! so i need your help

The reason it’s not excluding the values is that you’re checking whether the entire line is equal to the string '-9999.000000'. None of the lines are '-9999.000000', but some contain it. Also, none of the lines are a number; they’re all strings.

How best to do it depends on what you’re needing them for. If you want the numbers as a list of numbers, just convert them to numbers and don’t add any to the list that are -9999.

checking whether the entire line is equal to the string '-9999.000000' .

Okay Good. I’ve edited the code to be:

list_numbers = []
with open('\\Desktop\\Top1.txt', 'r') as file:
    for line in file:
        st_line = line.split()
        
        for value in st_line:
            if value == '-9999.000000' or value.startswith("-"):
                continue
            float_num = (float(value))
            list_numbers.append(float_num)

# print(list_numbers)
# print(type(value))
    

Worked for now, Thank you for the hint.
If you can help a brother as I need a little help in Matplotlib as:

I need to give these values as the Z dimension. √ Done
and X,Y to be a simple meshgrid ranged (1: len(values))

literally ,

0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 etc

AND The Z is given for each cross point between X,Y! Do you have any hints as i tried to do it
It’s like impossible!

'-9999.000000' starts with '-', so you don’t need to test for value == '-9999.000000'.

I have only limited knowledge of matplotlib.

1 Like

Okay Thank you Matthew :slight_smile: