0

I am having the following input txt file:

17,21.01.2019,0,0,0,0,E,75,meter tamper alarm
132,22.01.2019,64,296,225,996,A,,
150,23.01.2019,63,353,351,805,A,,
213,24.01.2019,64,245,244,970,A,,
201,25.01.2019,86,297,364,943,A,,
56,26.01.2019,73,678,678,1437,A,,
201,27.01.2019,83,654,517,1212,A,,
117,28.01.2019,58,390,202,816,A,,
69,29.01.2019,89,354,282,961,C,,
123,30.01.2019,53,267,206,852,A,,

Need to make a python program that can parse through the file. I need to find all the lines not containing A or C and output those lines in a new file. I'm completely stuck after trying several regex :( can you help me ?

2
  • Read the file line by line, split it by commas and check if any of the elements is A or C. Commented Feb 17, 2020 at 6:38
  • Can't you just use if "string" in lineString: and check if it does not contain both "A" and "C" Commented Feb 17, 2020 at 6:50

2 Answers 2

3

Try

with open('filename') as f:
    for line in f.readlines():
        if 'A' not in line or 'C' not in line:
            print(line)

OR Better, as your file content seems to resemble a csv(Comma Seperated Values) format, use pandas for better manipulations

Read the file

import pandas as pd
df = pd.read_csv('filename', header=None, sep=',')

     0           1   2    3    4     5  6     7                   8
0   17  21.01.2019   0    0    0     0  E  75.0  meter tamper alarm
1  132  22.01.2019  64  296  225   996  A   NaN                 NaN
2  150  23.01.2019  63  353  351   805  A   NaN                 NaN
3  213  24.01.2019  64  245  244   970  A   NaN                 NaN
4  201  25.01.2019  86  297  364   943  A   NaN                 NaN
5   56  26.01.2019  73  678  678  1437  A   NaN                 NaN
6  201  27.01.2019  83  654  517  1212  A   NaN                 NaN
7  117  28.01.2019  58  390  202   816  A   NaN                 NaN
8   69  29.01.2019  89  354  282   961  C   NaN                 NaN
9  123  30.01.2019  53  267  206   852  A   NaN                 NaN

Output

print(df[~df[6].str.contains('A|C', regex=True)])

    0           1  2  3  4  5  6     7                   8
0  17  21.01.2019  0  0  0  0  E  75.0  meter tamper alarm
Sign up to request clarification or add additional context in comments.

1 Comment

Updated. Please check @Guy
0

Try:

with open(r'file.txt', 'r') as f:
    for line in f:
        if 'A' not in line or 'C' not in line:
            print(line)

3 Comments

You are simply great, thank you for those fast responses! Another small question is there a way also to exclude any negative numbers?
@SakrayaamiS not as simple as it is right now. like this you are only comparing strings. you could search for '-' not in line. but for the I would recommend to use pandas like @Vishnudev did
you could split at every , and try to cast it to float and check if its negative

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.