Python parsing the lines from a file
Question: New to python. I'm reading from file line by line:

    
    
    with open("graph.txt", "r") as f:
        comList = f.readlines()
        print(comList)
        edge_u = [x[0] for x in comList]
        edge_v = [x[1] for x in comList]
    

graph.txt has :

> [(0, 7), (1, 9), (1, 9), (2, 0)]
>
> [(2, 1), (2, 1), (3, 6)]

I was expecting that readlines will parse the file line by line hence will
parse the text as a list of list. But its parsing it as List of strings. Hence
i;m unable to perform the other two operations. Tried to print edge_u and get
to know what its parsing. How to deal with this? Thanks.


Answer: You may try this:

    
    
    import ast
    
    with open("test.txt", "r") as f:
        for line in f:
            li = ast.literal_eval(line)
            edge_u = [x[0] for x in li]
            edge_v = [x[1] for x in li]
    

