-1

I am trying to convert this file https://bpa.st/SNPQ into a CSV file. I have spent a long time putting this code together but I don't understand why it is failing.

import re
f = open('PRM93052012100845','r')
text = f.readlines()
text = re.sub('^\n|\n$','',text)
for no,line in enumerate(text.splitlines()):
    print('"'+'","'.join([i.replace('"','\\"').strip() for i in re.split('(?<=^[0-9]{2})([0-9]{13}| {13})|  +',text.splitlines()[no].strip()) if i != None])+'"')

(there many more lines than what is in the paste sample)

I get the following output:

Traceback (most recent call last):
  File "/home/ben/Documents/WDL Content/eCom Python/printtest.py", line 5, in <module>
    text = re.sub('^\n|\n$','',text)
  File "/usr/lib/python3.8/re.py", line 210, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object

I wish I could say there is some erroneous data within the file but there is an MS Office Macro that completing the same objective. I would love to know what I am doing wrong.

quamrana
  • 37,849
  • 12
  • 53
  • 71

1 Answers1

1

text = f.readlines() gives you list, hence you cannot use it with re.sub as it expects string or bytes. replace it with text = f.read()

hack3r-0m
  • 700
  • 6
  • 20