2

Here is my code: def main():

    if len(sys.argv) != 3:

        fname = "OP.tex"
        dot_ind = fname.index(".") + 1
        ofname = fname[:dot_ind] + "1." + fname[dot_ind:]

    else:

        fname = sys.argv[1]
        ofname = sys.argv[2]
    print (fname, "+",ofname)
    writeout(ofname, replacement(readin(fname)))
def replacement(content):
   pattern = re.compile(r'(?<=\\\\\[-16pt]\n)([\s\S]*?)(?=\\\\\n\\thinhline)')
   re.findall(pattern, content)

if __name__ == "__main__":
    main()

For which I am receiving the following error:

File "test.py", line 30, in main
writeout(ofname, replacement(readin(fname)))
File "/home/utilities.py", line 138, in writeout
out.write(content)
TypeError: argument 1 must be string or read-only character buffer, not None

writeout is a function in another file that tells the python where to output the content as so:

def writeout(filename, content, append=False):
    """
    Writes content to file filename.
    """

    mode = "w"

    #append to the file instead of overwriting
    if append:
        mode = "a"

    #write content
    with open(filename, mode) as out:
        out.write(content)

The fname and ofname (input and output file names) are correct so why does the program say that the argument is None when it is a string?

Thank you so much.

1 Answer 1

6

Your replacement function must return a string and currently it returns None. So, change the statement

re.findall(pattern, content)

to something like

return ' '.join(re.findall(pattern, content))

Note you can't just return the result of re.findall, as that's a list, not a string; you must make a list out of that result, one way or another.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.