I have a txt file that I read with:
fdata =[]
with open(fn,'rb') as fo:
for i in xrange(1):
fo.next()
for line in fo:
data = line.split(',')
data = data[:23]
fdata.append(data)
print fdata
I would like convert the entire table to numbers from string. AND limit the table size to read to XXX lines.
To limit lines, try iterating using a counter and
readlines():To convert a string to a number (an integer in this case.
float()is for floating points):So to make your table only include integers, convert each line (after splitting) into an integer.
int()only returns an integer, so it doesn’t modify your variable. Replacingfdata.append(data))withfdata.append(int(data()should do the trick.Okay, to convert your entire list into integers, just iterate over each of those 23 elements to convert them into integers. This works for an arbitrary list of strings:
To convert your list into integers, but to exclude your first element, loop through it: