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

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Replacement of the code given below


    Code:
    #!/usr/bin/env python
    import csv
    
    list = []
    x = open("file.txt")
    with x as csv_data:
        entries = csv.reader(csv_data, delimiter=",")
        for entry in entries:
             list.append({
                 "name": entry[0],
                 "type": entry[1],
                 "link": entry[2],
                 "level": entry[3]
             })
    The above code is working fine, but it is not working inside gdb prompt. There is some issue with the keyword 'with' and 'as' support inside gdb. So can the same task be completed(what above code is doing) in an alternative way which does not use both keyword.
    In file file.txt, the contents are following:
    Mac, char, list, one
    John, char, list, three
    ...
    ...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    12
    umm... just don't use "with...as... :" And use either code or highlight tags for crying out loud! Reading un-indented Python is such a turnoff that I doubt anyone will respond to this.
    python Code:
    x = open("file.txt")
    entries = csv.reader(x, delimiter=",")
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by zxq9
    umm... just don't use "with...as... :" And use either code or highlight tags for crying out loud! Reading un-indented Python is such a turnoff that I doubt anyone will respond to this.
    python Code:
    x = open("file.txt")
    entries = csv.reader(x, delimiter=",")

    Thanks for your quick response. I have corrected the indentation.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by baijnath
    Thanks for your quick response. I have corrected the indentation.
    I tried this, but not working,

    Code:
    #!/usr/bin/env python
    import csv
    import gdb
    import sys
    import future
    list = []
    x = open("b.txt")
    
    entries = csv.reader(x, delimiter=",")
    for entry in entries:
            list.append({
                "name": entry[0],
                "type": entry[1],
                "link": entry[2],
                "level": entry[3]
            })
    filtered_list = [entry for entry in list if entry['name'] == "Mac"]
    for entry in filtered_list:
            print entry['name']
            print entry['type']
            print entry['link']
            print entry['level']
    The error is:
    File "./file.py", line 20
    print entry['name']
    ^
    SyntaxError: invalid syntax
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,840
    Rep Power
    480
    You've run a python 2 program with python 3.
    python3 print is a function.
    python2 print is a statement.

    As a function is more useful.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    12
    python Code:
    # Do this
    print(entry['name'])
    # Not this
    print entry['name']

    Originally Posted by b49P23TIvg
    As a function is more useful.
    Very true. True enough that using it as a function in Python2 is recommended as well:
    python Code:
    >>> bedevere = "And that, sir, is how we know the Earth to be banana-shaped."
    >>> print bedevere
    And that, sir, is how we know the Earth to be banana-shaped.
    >>> from __future__ import print_function
    >>> print(bedevere)
    And that, sir, is how we know the Earth to be banana-shaped.

    Doing this (among other __future__ imports) not only brings in useful features, it reduces the number of idioms one must keep in mind.

IMN logo majestic logo threadwatch logo seochat tools logo