I'm trying to extract all the phone numbers from a CSV document and append them to a list in string format. Here is a sample of my input:
[email protected],John,Doe,,,(555) 555-5555
And here is the code I am using:
l = []
with open('sample.csv', 'r') as f:
reader = csv.reader(f)
for x in reader:
number = re.search(r'.*?@.*?,.*?,.*?,.*?,.*?,(.*?),',x)
if number in x:
l.append(''.join(number))
Basically, I'm trying to check if there is a number at a certain position in the row (where the parentheses are) and then append that to a list as a string using join. However, I keep getting this error:
Traceback (most recent call last):
File "C:/Users/svillamil/Desktop/Final Phone.py", line 14, in <module>
number = re.search(b'.*?@.*?,.*?,.*?,.*?,.*?,(.*?),', x)
File "C:\Users\svillamil\AppData\Local\Programs\Python\Python36-32\lib\re.py", line 182, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object
How do I get around this?
xis not a string. Look at the documentation for thecsvlibrary.