0

I need to know if a variable in Python is a string(name) or a number. I want to check out the degree of graphs, however I need to know if "i" iterator, is a number inside of the string or a name inside of the string, showing the degree of graphs at the end.

Is there something wrong in this code?

import csv, sys
import networkx as nx

def ministro_lei():

    stf = csv.reader(open('resultset.csv', 'rb'), delimiter=',', quotechar='|')
    eds = [(i[0],i[1],i[2]) for i in stf]
    G = nx.DiGraph(nome='ministro_lei')
    G.add_weighted_edges_from(eds)
    for i in G.degree():
        if isinstance(i,str):
            print (" This is a name:", i)
        elif isinstance(i, int):
            print ("This is a number: ", i)
        else:
            raise ValueError 
    return G, eds

ministro_lei = ()

Here lies some examples of outs:

  "MIN. OCTAVIO GALLOTTI",53,109
  "MIN. SYDNEY SANCHES",13,109
  "MIN. JOAQUIM BARBOSA",101,108

Please any help? Thanks

5
  • 2
    Could you be more precise in what you are asking? It's totally unclear at least for me what do you want. Commented Nov 11, 2011 at 12:36
  • 1
    Your "normal" return at the end of the method ministro_lei returns two variables. However, there are two more return (i) lines within the loops, that return single variables. This structure will never pass past the first iteration of the for-loop. Is this what you want? Commented Nov 11, 2011 at 12:37
  • I is a list. Not an int or a string Commented Nov 11, 2011 at 13:24
  • That is right, a list indeed..thank you joel.. Commented Nov 11, 2011 at 13:25
  • If one of the answers works for you, you should accept it (click on the greet checkmark by it). Commented Oct 4, 2012 at 17:01

2 Answers 2

2

They must be strings since ",".join(i) would have returned a TypeError if i were an int.

But, in general, to find out the type of a variable, use type:

print(type(i))
Sign up to request clarification or add additional context in comments.

2 Comments

it looks to me like i is a list. Not a string or an integer.
@joelgoldstick: Hm. The code has changed after I posted my answer.
1

I hope this helps

>>> s = 'hello'
>>> type(s)
<type 'str'>
>>> type(s) is str
True
>>> n = 6
>>> type(n)
<type 'int'>
>>> type(n) is int
True

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.