Python: string join multiple attributes from object -
i have orm based object list. want concatenate attributes delimited "|" (pipe) , concatenate objects using "\n".
i tried:
class a(object): def __init__(self, name, age): self.name = name self.age = age obj_list = [a("james", 42), a("amy", "23")] "\n".join("|".join(o.name, o.age o in obj_list)) file "<console>", line 1 syntaxerror: generator expression must parenthesized if not sole argument
what must parenthesized?
any hints?
tank you.
i think wanted achieve:
obj_list = [a("james", 42), a("amy", "23")] "\n".join("|".join((o.name, o.age)) o in obj_list)
result:
james|42 amy|23
note: if object contains non-string attributes, have convert strings, e.g. "|".join(o.name, str(o.age))
.
Comments
Post a Comment