newbie question(file-delete trailing comma)

Mikael Olofsson mikael at isy.liu.se
Wed Feb 28 04:38:02 EST 2007


kavitha thankaian wrote:
> my script writes a dictionary to a file.but i need only the values 
> from the dictionary which should be sepearted by a comma,,,so i did as 
> following:
> [snip code that generates the incorrect original file]
> when i execute the above code,my test.txt file has the following:
> a,b,c,d,
> now i need to delete the comma at the end,,,this is my problem,,,

This is the first time you mention that you have control over the 
generation of the original file. Then I suggest that you fix the problem 
before generating the file. For instance, consider the following 
interactive session.

 >>> some={1:'a',2:7,3:'c',4:'d'}
 >>> some
{1: 'a', 2: 7, 3: 'c', 4: 'd'}
 >>> some.values()
['a', 7, 'c', 'd']
 >>> [str(x) for x in some.values()]
['a', '7', 'c', 'd']
 >>> ','.join([str(x) for x in some.values()])
'a,7,c,d'

Then write that to your file instead.

/MiO



More information about the Python-list mailing list