Hi,
I have the following in a text file:
1,a
1,b
1,b
2,a
2,c
2,a
2,c
etc....
I have the following code to open the text file create a list from the data inside. I am trying to create a dictionary like:
{[1:a], [1:b], [1:b], [2:a], [2:c], [2:a], [2:c]}
I am using the following:
[CODE=python]
infile = open('input.txt ', 'r')
records = infile.readline s()
infile.close()
records = [s.replace('\n', '') for s in records]
finalrecords = map(string.spli t() ,records)
[/CODE]
However I keep getting the following error:
"pythontest.py" , line 5, in <module>
finalrecords = map(string.spli t() ,records)
NameError: name 'string' is not defined
Any advice - also moving forward I would like to create from the dictionary a count associated with each unique instance of a key:value relationship so using the above data I would write to a file:
KEY UNIQUE INSTANCES
1 2 (sum for unique key value instance 1:a and 1:b)
2 2 (sum for unique key value instance 2:a and 2:c)
I can do this in SQL but would prefer to do in python for speed and flexibility with computations.
Any advice is greatly appreciated.
GTXY20
I have the following in a text file:
1,a
1,b
1,b
2,a
2,c
2,a
2,c
etc....
I have the following code to open the text file create a list from the data inside. I am trying to create a dictionary like:
{[1:a], [1:b], [1:b], [2:a], [2:c], [2:a], [2:c]}
I am using the following:
[CODE=python]
infile = open('input.txt ', 'r')
records = infile.readline s()
infile.close()
records = [s.replace('\n', '') for s in records]
finalrecords = map(string.spli t() ,records)
[/CODE]
However I keep getting the following error:
"pythontest.py" , line 5, in <module>
finalrecords = map(string.spli t() ,records)
NameError: name 'string' is not defined
Any advice - also moving forward I would like to create from the dictionary a count associated with each unique instance of a key:value relationship so using the above data I would write to a file:
KEY UNIQUE INSTANCES
1 2 (sum for unique key value instance 1:a and 1:b)
2 2 (sum for unique key value instance 2:a and 2:c)
I can do this in SQL but would prefer to do in python for speed and flexibility with computations.
Any advice is greatly appreciated.
GTXY20
Comment