removing /n in dictionary

  • A+
Category:Languages

I've got a problem with removing /n in my program here is the code

with open(filename) as f:     for line in f.readlines():         parent, child = line.split(",")             parent.strip()             child.strip()             children[child].append(parent) 

tried using .rstrip and other variants but it does nothing for my, this is the result i get

{'Patricia/n': ['Mary'], 'Lisa/n': ['Mary']}  

the problem is when i call children["Patricia"] i get [], because it recognizes only children["Patricia/n"]

 


Please use strip before split:

parent, child = line.rstrip("/n").split(",") 

Issue was: parent.strip() needs to be re-assigned to a string as strings are immutable.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: