Multi-arrays python

Piet van Oostrum piet at cs.uu.nl
Wed Oct 14 14:53:09 CEST 2009


>>>>> bbarbero at inescporto.pt (b) wrote:

>b> Hi again!
>b> After testing the whole day, I have got my goals from the last email,  but
>b> as always, another issues came up! and now that Ive been able to  save a
>b> list of list (or multi-arrays) as below :

>b> ['100.mp3\n' '10008.mp3\n' '10005.mp3\n' '10001.mp3\n' '10006.mp3\n']
>b> ['10001.mp3\n' '10005.mp3\n' '100.mp3\n' '10008.mp3\n' '10006.mp3\n']
>b> ['10005.mp3\n' '10001.mp3\n' '100.mp3\n' '10008.mp3\n' '10006.mp3\n']
>b> ['10006.mp3\n' '10005.mp3\n' '10001.mp3\n' '100.mp3\n' '10008.mp3\n']
>b> ['10008.mp3\n' '100.mp3\n' '10001.mp3\n' '10005.mp3\n' '10006.mp3\n']

>b> I am not able to manipulate it again! I read it with:
>b> Myfile.read() and all what I get is a str type data, what make my aim  very
>b> difficult to reach!  What I want, is just to read one line(one  specific
>b> line, so  I wouldnt have to read the whole file) and to get  the numbers of
>b> the songs from that line. Maybe I should save the  information in another
>b> way... But I just get those lines as lists, and  write them in a file. Is
>b> there a better way? I am very receptive to  suggestions! Thanks again for
>b> your help!

You are unclear about the syntax of your file. What is a line? Is this a
line?
['100.mp3\n' '10008.mp3\n' '10005.mp3\n' '10001.mp3\n' '10006.mp3\n']

If so what do the \n after the filenames mean? Is \n a newline, or is it
a backslash followed by 'n'? In the first case then every filename is on
a separate line. So then you are mixing two concepts of line.

Anyhow you would make your life easier by getting rid of the \n's.

If all your filenames are numeric with the extension mp3 then the
following gives you a list of the filenames from a line:

In [31]: re.findall('[0-9]+\.mp3', line)
Out[31]: ['100.mp3', '10008.mp3', '10005.mp3', '10001.mp3', '10006.mp3']

-- 
Piet van Oostrum <piet at vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]



More information about the Python-list mailing list