#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    Call function on each line in list


    Code:
    view_file = open('FILE.TXT', 'r')
    detail_data = {}
    
    def build_data_point(line_index, index_list):
        data_point = ''
        for i in index_list:
            data_point = data_point + line_index[i]
        # return ''.join(data_point.split())
        return data_point.replace("  ", "")
    
    
    def bulk_data(line):
        detail_data["a"] = build_data_point(line, range(0, 2))
        detail_data["b"] = line[2]
        detail_data["c"] = build_data_point(line, range(3, 7))
        detail_data["d"] = build_data_point(line, range(18, 26))
        detail_data["e"] = line[26]
        detail_data["f"] = build_data_point(line, range(27, 35))
        detail_data["g"] = build_data_point(line, range(35, 47))
    
    
    
    lines = []
    for line in view_file:
        lines.append(line)
    
    
    for data in lines:
        bulk_data[data]
    
    
    print detail_data

    Hi guys, I'm having trouble processing the individual elements in my list and any help would really be appreciated. I can process one line at a time using
    Code:
    bulk_data(lines[1])
    but after iterating through the list I'm getting all sorts or errors (passed in too many args, 'function' object is not subscriptable). Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    To be able to help, could you please provide:

    1. Example input data (FILE.TXT)
    2. All error messages you get
    3. Sample of desired output data (detail_data)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0
    An example line of the file is:
    G 089301 0364545708 00001000HHH OK009895 1000000010001000 Graphic/ I00000000 01
    The desired output is something like:
    {'a': 'G 089301', 'b': ' 0364545708', 'c': '00001000HHH', 'd': 'OK009895 1000000010001000', 'e': '100000001000100', 'f': 'Graphic/ I00000000 01',
    All lines in the file are of the same format and work fine when I individually pass them into the function I would now like to bulk process them.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    148
    Rep Power
    139
    Originally Posted by donfanzu23
    All lines in the file are of the same format and work fine when I individually pass them into the function I would now like to bulk process them.
    Shouldn't be there some kind of delimiter between each pair of columns rather than relying on positions in order to detect each token? I ask the question because you said each line has the same format, which I assume means the same number of columns, but that does not necessarily means that all of them have the same length.

    Regards,
    Dariyoosh
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0
    Hi guys,

    The file is really in a horrendous format with big gaps between text, that didn't really come across when I posted a sample of the data. So I chucked everything into a list and am processing it on a character by character basis. I works fine. It only this part
    Code:
    for data in lines:
        bulk_data[data]
    that I am having trouble with. It seems like a very simple thing to do and probably is but I can't figure it out. thanks
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by donfanzu23
    Hi guys,

    The file is really in a horrendous format with big gaps between text, that didn't really come across when I posted a sample of the data. So I chucked everything into a list and am processing it on a character by character basis. I works fine. It only this part
    Code:
    for data in lines:
        bulk_data[data]
    that I am having trouble with. It seems like a very simple thing to do and probably is but I can't figure it out. thanks
    The problem is that "bulk_data" is a function, and you're trying to use it as a list or map.

    Change to:
    Code:
    bulk_data(data)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0
    Oh yes that was a typo, I meant the way you had it.

    Code:
    for idx, val in enumerate(lines):
        bulk_data(line[idx])
    This is what im trying at the moment but I get the exact same error "IndexError: string index out of range"
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by donfanzu23
    Oh yes that was a typo, I meant the way you had it.

    Code:
    for idx, val in enumerate(lines):
        bulk_data(line[idx])
    This is what im trying at the moment but I get the exact same error "IndexError: string index out of range"
    Please post your full code again.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0
    Code:
    view_file = open('FILE.TXT', 'r')
    detail_data = {}
    
    def build_data_point(line_index, index_list):
        data_point = ''
        for i in index_list:
            data_point = data_point + line_index[i]
        # return ''.join(data_point.split())
        return data_point.replace("  ", "")
    
    
    def bulk_data(line):
        detail_data["a"] = build_data_point(line, range(0, 2))
        detail_data["b"] = line[2]
        detail_data["c"] = build_data_point(line, range(3, 7))
        detail_data["d"] = build_data_point(line, range(18, 26))
        detail_data["e"] = line[26]
        detail_data["f"] = build_data_point(line, range(27, 35))
        detail_data["g"] = build_data_point(line, range(35, 47))
    
    
    
    lines = []
    for line in view_file:
        lines.append(line)
    
    
    for idx, val in enumerate(lines):
    bulk_data(line[idx])
    
    
    print detail_data
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Indent second line and change line[idx] to lines[idx].

    Code:
    for idx, val in enumerate(lines):
    bulk_data(line[idx])

IMN logo majestic logo threadwatch logo seochat tools logo