arrays - Split a string within a list python -
i have list in python, text document split @ new line characters. end of data indicated #
in text document. need count each of element of list , split strings in list @ tab characters, creating 2 dimensional list.
i thought simple, however, i'm not getting result code. strings in list not splitting @ all, nevermind @ \t
.
with open('names.txt') names: records = names.read().split('\n') recordcount = 0 item in records: if item != '#': recordcount += 1 item = item.split('\t') print (records) print (recordcount)
has got tab characters being troublesome? or can not replace elements of list in-place? should creating new list split records?
you're reassigning local variable. doesn't affect contents of list. try this:
for i, item in enumerate(records): if item != '#': recordcount += 1 records[i] = item.split('\t')
Comments
Post a Comment