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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
prax0r
  • 46
  • 4
  • `open( 'b.txt', 'w').write( '\n'.join( i.strip() for i in open('a.txt').readlines() if not i.startswith('"') ) – lenik Jul 06 '18 at 09:45

1 Answers1

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']
Sunitha
  • 11,777
  • 2
  • 20
  • 23
  • 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 Jul 06 '18 at 09:46
  • @lenik. Thanks for the comment. I have updated the answer – Sunitha Jul 06 '18 at 09:54
  • @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. – prax0r Jul 06 '18 at 09:59