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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -