Filter txt lines that start with a specific string (Python)

159
November 03, 2019, at 05:40 AM

I have a txt file that looks like this:

...
|J150|DRE.16.2|T|2|DRE.16|PROVISAO P  CSLL|6779,24|D|D||
|J150|DRE.16.2.001|D|3|DRE.16.2|CSLL|6779,24|D|D||
|J150|DRE.17|T|1||LUCRO DO EXERCICIO|55797,1|C|R||
|J005|01012018|31122018|1||
|J100|BP.01|T|1||A|ATIVO|5540527,48|D|8656252,32|D||
|J100|BP.01.1|T|2|BP.01|A|ATIVO CIRCULANTE|5030370,68|D|7881200,94|D||
|J100|BP.01.1.1|T|3|BP.01.1|A|DISPONIBILIDADES|380741,7|D|777224,63|D||
|J100|BP.01.1.1.01|T|4|BP.01.1.1|A|CAIXA|96786,62|D|69935,41|D||
|J100|BP.01.1.1.01.001|D|5|BP.01.1.1.01|A|Caixa|96786,62|D|69935,41|D||
...

It is quite long. I want to separate in a new file only the lines that start with "|J100|". I've tried some of the answers here but didn't work in my case. Below my trials:

path="file.txt"
open('newfile','w').writelines([ line for line in open(path) if '|J100|' in line])

Didn't work, got UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc1 in position 255: invalid start byte

Then I tried this one:

with open(path,'rb') as f,open('new.txt','wb') as g:
    g.writelines(filter(lambda line: '|J100|' in line, f))

And got this as response: TypeError: a bytes-like object is required, not 'str'

Any ideas?

Rent Charter Buses Company
READ ALSO
Aren't Python strings immutable? Then why does a + “ ” + b work?

Aren't Python strings immutable? Then why does a + “ ” + b work?

My understanding was that Python strings are immutable

146
I get Very Different result by keras_metrics and sklearn.metrics

I get Very Different result by keras_metrics and sklearn.metrics

I am trying to classify text dataI am using keras_metrics to get precision, recall and f1 score

127
Tensorflow Object Detection API - Error running model_builder_test.py module 'tensorflow' has no attribute 'contrib'

Tensorflow Object Detection API - Error running model_builder_test.py module 'tensorflow' has no attribute 'contrib'

I installed the Tensorflow Object Detection API, and ran the model_builder_testpy script to make sure everything was working

138
convert red text into black opencv python

convert red text into black opencv python

I have an Image having some sort of Reading's in Seven Segment formatI want to convert(my output) Red color Text into Black

159