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 7879479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:54:20+00:00 2026-06-03T03:54:20+00:00

I have a list of rules for a given input file for my function.

  • 0

I have a list of rules for a given input file for my function. If any of them are violated in the file given, I want my program to return an error message and quit.

  • the first line should start with a ‘#’ symbol (indicating a header line)
  • every line should have exactly 10 columns
  • column 2 (counting from 0) should be either a + or – symbol
  • column 8 should be a comma-separated list of integers
  • column 9 should be a comma-separated list of integers, with exactly the same number of integers as column 8.

To attempt to do this I have written the following code:

Edited steveha’s answer into code

with open(infile, 'r') as fp:
line = fp.readline().strip()
if not line.startswith('#'):
    print ('First line does not start with #')
    sys.exit(1)
n = 1
for line in fp.readlines():
    d = '(\d+,\d+)'
    n += 1
    cols = line.strip().split()
    i = search(d, line)
    if len(cols) != 10:
        print('Lenth not equal to 10')
        sys.exit(1)
    if cols[2] != '+' or '-':
        print('Column 2 is not a + or - symbol')
        sys.exit(1)
    if i and cols[8] != i.group(1):
        print('Column 8 is not a comma-separated list of integers')
        sys.exit(1)
    if i and cols[9] != i.group(1) and len(cols[9]) != len(cols[8]):
        print('Column 9 in not a comma-separated list of integers with the exact same number of integers in column 8')
        sys.exit(1)

Yet, when I run this, not all the conditionals appear to be functional. I am trying to do this correctly?

  • 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-03T03:54:20+00:00Added an answer on June 3, 2026 at 3:54 am

    This line is wrong:

    if cols[2] != '+' or '-':
    

    This would be correct:

    if not (cols[2] == '+' or cols[2] =='-'):
    

    I would suggest this instead:

    if cols[2] not in ('+', '-'):
    

    Also, I suggest you not call fp.readlines(). The file object fp works as an iterator, and when you iterate it you get one line at a time. So your loop would then become:

    for line in fp:
    

    Also, it looks like n is keeping track of line number? In that case, there is an idiomatic Python way you can do it, like so:

    for n, line in enumerate(fp, 1):
    

    enumerate() takes an iterator and returns the next value from the iterator together with an incrementing count. By default the count starts at 0, but you can optionally specify a starting number, as I did here to make it start at 1.

    And it is best practice in Python to use the with statement to open files, so I suggest you do this:

    with open(infile, 'r') as fp:
        line = fp.readline().strip()
        if not line.startswith('#'):
            print ('First line does not start with #')
            sys.exit(1)
        for line in fp:
            # process lines here
    

    The code you are showing does not fully make sense to me. This line:

    i = search(d, line)
    

    You must have already done a from re import search command. I actually recommend just doing import re and then explicitly calling re.search() but I guess that is a matter of preference. Anyway, this sets i to the match group result from re.search() (or to None if the match fails). But later on in the code you are testing r rather than i, and you never set r in any code we see here so I am not sure what that will do. Personally I use m as the variable name for a match group.

    Your regular expression just matches a pair of positive integers. Nothing there counts how many integers there are. len(cols[8]) is checking how many characters in cols[8].

    You are calling a string method function .split(''), which is not correct. On my system it raises an exception: ValueError: empty separator Just call .split() to split on white space; I’ll assume that the comma-separated integers lists must not have any white space.

    Finally, please consider the guidelines in PEP 8. Your variable FirstLine is capitalized like a class name rather than a variable name; that didn’t exactly confuse me, but it was sort of distracting. Most of the Python community follows PEP 8.

    http://www.python.org/dev/peps/pep-0008/

    Taking all of the above into account, I simply re-wrote your code:

    import sys
    
    def parse_list_of_int(s):
        try:
            return [int(x) for x in s.split(',')]
        except Exception:
            return None
    
    with open("test.txt", 'r') as f:
        # read line 1
        line = f.readline().strip()
        if not line.startswith('#'):
            print ('First line does not start with #')
            sys.exit(1)
    
        # need to start enumerate() at 2 because we pulled line 1 out above
        for i, line in enumerate(f, 2):
            cols = line.strip().split()
            if len(cols) != 10:
                print('line {0}: Length not equal to 10'.format(i))
                sys.exit(1)
            if cols[2] not in ('+', '-'):
                print('line {0}: Column 2 is not a + or - symbol'.format(i))
                sys.exit(1)
            lst8 = parse_list_of_int(cols[8])
            if lst8 is None:
                print('line {0}: Column 8 is not a comma-separated list of integers').format(i)
                sys.exit(1)
            lst9 = parse_list_of_int(cols[9])
            if lst9 is None:
                print('line {0}: Column 9 is not a comma-separated list of integers'.format(i))
                sys.exit(1)
            if len(lst8) != len(lst9):
                print('line {0}: Column 8 and column 9 do not have same number of integers'.format(i))
                sys.exit(1)
    
    print('No problems!')
    sys.exit(0)
    

    I wrote a simple function to parse out the list of integers, build a Python list, and return it. Then the code can actually check properly whether the two lists are the same length.

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

Sidebar

Related Questions

I have a list of rules for a given input file for my function.
I have a list of rewrite rules, but one of the rules is applying
I have the following method and interface: public object ProcessRules(List<IRule> rules) { foreach(IRule rule
I have a make rule which generates a dependencies file for a list of
I have List I want to sort Desc by Priority, which is int and
I have a list of UTF-8 strings that I want to sort using Enumerable.OrderBy
I want to refine the raw text by using regular expression, given a list
I have the following rules in my htaccess: RewriteRule ^([^/.]+)/?$ list.php?categoryShortForm=$1&locationShortForm=world [QSA] RewriteRule ^([^/.]+)/([^/.]+)/?$
I have a table that is storing a list of rules. In my code,
Does anyone have a list of rough rule-of-thumb estimators for the various data structures?

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.