Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7982583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:42:54+00:00 2026-06-04T10:42:54+00:00

I’m writing the following function to format some text from a file object ‘item’:

  • 0

I’m writing the following function to format some text from a file object ‘item’:

def clean(item):
    lines = open(str(item)).readlines()
    outp = []
    switch = 1

    for line in lines:
        line = line.strip()
        if line:
            line = re.sub(' +', ' ', line)
            if '%' in line:
                line = line.replace('%', ' per cent')
            if line.startswith('Note'):
                switch = 2

            if line.startswith('l\t'):
                line = line.split('\t')
                line[0] = '<@01_bullet_point>l<@$p>'
                line = '\t'.join(line)
                outp.append(get_prefix(switch,1) + line)
            else:
                outp.append(get_prefix(switch,2) + line)
            outp.append('\n')
    return ''.join(outp)

I’m getting a TypeError: unsupported operand types for +: ‘NoneType’ and ‘str’

I googled around for solutions but found http://www.skymind.com/~ocrow/python_string/, whose solution 4 seemed to confirm that I was on the right track.

The other function(s) this is referencing are confirmed working so I know the problem must be in here but, having tried to find related SO questions and checked my copy of Python in a Nutshell, I can’t figure out why I’m getting the error. My best guess was something to do with variable scope but I can’t see it and, again, searching turned up nothing I could understand as the same problem. I expect I’m missing something obvious but any help would be much appreciated.

EDIT
get_prefix is:

def get_prefix(section, type):
    if section == 1:
        if type == 1:
            prefix = '@01_bullets:'
        elif type == 2:
            prefix = '@04_section_sub_subhead:'
        else:
            prefix = '@06_body_text:'

    else:
        if type == 2:
            prefix = '@14_notes_sub_sub_heading:'
        else:
            prefix = '@16_notes_text:'

    return prefix

I can’t see how it might return None.

The full traceback is:

File "rep_builder.py", line 65, in <module>
`rep.write(clean(item))`
File "rep_builder.py", line 36, in clean
`outp.append(get_prefix(switch,2) + line)`
TypeError: unsupported operand types for +: 'NoneType' and 'str'

Sorry for slow updates I’m working across two unconnectable machines (long story).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:42:55+00:00Added an answer on June 4, 2026 at 10:42 am

    Check your indentation. Are you sure get_prefix doesn’t look something like this? (I ask because the indentation in clean is wrong):

    def get_prefix(section, type):
        if section == 1:
            if type == 1:
                prefix = '@01_bullets:'
            elif type == 2:
                prefix = '@04_section_sub_subhead:'
            else:
                prefix = '@06_body_text:'
    
        else:
            if type == 2:
                prefix = '@14_notes_sub_sub_heading:'
            else:
                prefix = '@16_notes_text:'
    
            return prefix
    

    Indented as above, get_prefix will return None if section equals 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.