Revision history [back]

Assuming that there is no problem with opening the file, here are some hints:

your list L contains the lines of the file f, which seems to be only one line. Hence L[0] is a string that looks like "0,1,0,1,1,....,1\n" with \n' standing for the line feed character.

Hence, f you want to extract the elements of that list, you have to remove that character, and then split the string to get a list, with something like:

sage: L[0].replace('\n','').split(',')
['0', '1', '0', '1', '1',..., '1']


But ten you have a list of characters, not integers, so you have to turn the elements of your list into integers:

sage: A = [ZZ(i) for i in L[0].replace('\n','').split(',')]
sage: A
[0, 1, 0, 1, 1, ..., 1]


Another (simpler) way is to use the csv module that deals well with coma separated values:

sage: r = csv.reader(f)
sage: A = [ZZ(i) for i in next(r)]
sage: A
[0, 1, 0, 1, 1, ..., 1]


Assuming that there is no problem with opening the file, here are some hints:

your list L contains the lines of the file f, which seems to be only one line. Hence L[0] is a string that looks like "0,1,0,1,1,....,1\n" with \n' \n standing for the line feed character.

Hence, f you want to extract the elements of that list, you have to remove that character, and then split the string to get a list, with something like:

sage: L[0].replace('\n','').split(',')
['0', '1', '0', '1', '1',..., '1']


But ten you have a list of characters, not integers, so you have to turn the elements of your list into integers:

sage: A = [ZZ(i) for i in L[0].replace('\n','').split(',')]
sage: A
[0, 1, 0, 1, 1, ..., 1]


Another (simpler) way is to use the csv module that deals well with coma separated values:

sage: r = csv.reader(f)
sage: A = [ZZ(i) for i in next(r)]
sage: A
[0, 1, 0, 1, 1, ..., 1]

 3 No.3 Revision slelievre 16999 ●21 ●151 ●335 http://carva.org/samue...

Assuming that there is no problem with opening the file, here are some hints:

Your list L contains the lines of the file f, which seems to be only one line. Hence L[0] is a string that looks like "0,1,0,1,1,....,1\n" with \n standing for the line feed character.

Hence, f you want to To extract the elements of that list, you have to remove that character, and then split the string to get a list, with something like:list:

sage: L[0].replace('\n','').split(',')
['0', '1', '0', '1', '1',..., '1']


But ten you have Then turn that list of strings into a list of characters, not integers, so you have to turn the elements of your list into integers:

sage: A = [ZZ(i) for i in L[0].replace('\n','').split(',')]
sage: A
[0, 1, 0, 1, 1, ..., 1]


Another (simpler) way is to use the csv module that deals well with coma comma separated values:

sage: r = csv.reader(f)
sage: A = [ZZ(i) for i in next(r)]
sage: A
[0, 1, 0, 1, 1, ..., 1]


Finally, the Python documentation about reading and writing files recommends using with to deal with files.

sage: f = open('path/abc.txt')

sage: with open('path/abc.txt', 'r') as f: