Reading from a file and converting it into a list of lines: code not working

John Machin sjmachin at lexicon.net
Tue Jun 6 01:18:08 EDT 2006


On 6/06/2006 2:10 PM, Girish Sahani wrote:
> I have a text file in the following format:
> 
> 1,'a',2,'b'
> 3,'a',5,'c'
> 3,'a',6,'c'
> 3,'a',7,'b'
> 8,'a',7,'b'

Check out the csv module.

> .
> .
> .
> Now i need to generate 2 things by reading the file:
> 1) A dictionary with the numbers as keys and the letters as values.
> e.g the above would give me a dictionary like
> {1:'a', 2:'b', 3:'a', 5:'c', 6:'c' ........}
> 2) A list containing pairs of numbers from each line.
> The above formmat would give me the list as
> [[1,2],[3,5],[3,6][3,7][8,7]......]
> 
> I wrote the following codes for both of these but the problem is that
> lines returns a list like ["1,'a',2,'b'","3,'a',5,'c","3,'a',6,'c'".....] 
>        Now due to the "" around each line,it is treated like one object
> and i cannot access the elements of a line.

You managed to split the file contents into lines using
     lines = open(filename).read().split("\n")
Same principle applies to each line:

|>>> lines = ["1,'a',2,'b'","3,'a',5,'c","3,'a',6,'c'"]
|>>> lines[0].split(',')
['1', "'a'", '2', "'b'"]
|>>> lines[1].split(',')
['3', "'a'", '5', "'c"]
|>>>

> 
> [code]
> #code to generate the dictionary
> def get_colocations(filename):
>     lines = open(filename).read().split("\n")
>     colocnDict = {}
>     i = 0
>     for line in lines:
>         if i <= 2:
>             colocnDict[line[i]] = line[i+1]
>             i+=2
>             continue
>         return colocnDict
The return is indented too far; would return after 1st line.
> [/code]
> 
> [code]
> def genPairs(filename):
>     lines = open(filename).read().split("\n")
>     pairList = []
>     for line in lines:
>         pair = [line[0],line[2]]
>         pairList.append(pair)
>         i+=2

i is not defined. This would cause an exception. Please *always* post 
the code that you actually ran.

>         continue
> return pairList

dedented too far!!

> [/code]

> Please help :((

def get_both(filename):
     lines = open(filename).read().split("\n")
     colocnDict = {}
     pairList = []
     for line in lines:
	n1, b1, n2, b2 = line.split(",")
	n1 = int(n1)
	n2 = int(n2)
         a1 = b1.strip("'")
         a2 = b2.strip("'")
         colocnDict[n1] = a1
         colocnDict[n2] = a2
         pairList.append([n1, n2])
     return colocnDict, pairList

def get_both_csv(filename):
     import csv
     reader = csv.reader(open(filename, "rb"), quotechar="'")
     colocnDict = {}
     pairList = []
     for n1, a1, n2, a2 in reader:
         n1 = int(n1)
         n2 = int(n2)
         colocnDict[n1] = a1
         colocnDict[n2] = a2
         pairList.append([n1, n2])
     return colocnDict, pairList

HTH,
John



More information about the Python-list mailing list