0

I'am currently writing a python programm thats extracs phone Numbers into a file.

Since the Data quality is not that great yet we have a few blank numbers. The file looks something like this:

xxxxx
1241515151
""
""
""
""
""
+43 2414141414
0221412414
""
01989797 8
214141

My Question is how do I remove the lines with "".

I tried:

f = open("fastlane.txt","r+")
d = f.readlines()
f.seek(0)
for i in d:
    if i != " ""$ ":
        f.write(i)
f.truncate()
f.close()

Thanks in advance.

1
  • `open( 'b.txt', 'w').write( '\n'.join( i.strip() for i in open('a.txt').readlines() if not i.startswith('"') ) Commented Jul 6, 2018 at 9:45

1 Answer 1

2

You can do this easily with regex

You can filter number and special chars containing + or - or space

import re
with open("fastlane.txt","r+") as f:
    re.findall(r'[\d +-]+', f.read())

# ['1241515151', '+43 2414141414', '0221412414', '01989797 8', '214141']

Or to filter everything except quotes and newline

re.findall(r'[^"\n]+', f.read())
# ['xxxxx', '1241515151', '+43 2414141414', '0221412414', '01989797 8', '214141']
Sign up to request clarification or add additional context in comments.

3 Comments

does not work with numbers that include anything beyond digits and spaces, like '1-800-NEED-HELP` or even `1-800-6453-6323', as a reminder, the original question was "how to remove lines with double quotes?"....
@lenik. Thanks for the comment. I have updated the answer
@Sunitha thanks for the answers, sadly it does not seem like this is working for my case. I edited the file and added some double quotes myself. Those are getting removed just fine. The ones created in the regular txt stay.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.