Chris
Chris

Reputation: 1219

Make a array from lines made up of multiple variables

Right now I have this.

print (name.text.strip(), genre.text.strip(), bouquets.text.strip(), encryption.text.strip(), sid.text.strip(), nid.text.strip(), tid.text.strip(), sep = '\t')

This gives the output I desire. How can I add that line to a array instead of printing it?

I have tried

channel = (name.text.strip(), genre.text.strip(), bouquets.text.strip(), encryption.text.strip(), sid.text.strip(), nid.text.strip(), tid.text.strip(), sep = '\t')
channels.append (channel)

But it doesn't work.

channel = (name.text.strip(), genre.text.strip(), bouquets.text.strip(), encryption.text.strip(), sid.text.strip(), nid.text.strip(), tid.text.strip(), sep = '\t')
                                                                                                                                                            ^

SyntaxError: invalid syntax

I think it's because of the tab separation maybe?

Does anyone know how to do this properly?

Upvotes: 0

Views: 37

Answers (2)

Jan Stránský
Jan Stránský

Reputation: 1691

If "properly" also mean reducing code duplication, you can use:

channel = (name, genre, bouquets, encryption, sid, nid, tid)
channel = "\t".join(item.text.strip() for item in channel)
channels.append(channel)

Upvotes: 0

damon
damon

Reputation: 15128

First you need to build the str and then it can be added to your array.

channel = "\t".join([
    name.text.strip(),
    genre.text.strip(),
    bouquets.text.strip(),
    encryption.text.strip(),
    sid.text.strip(),
    nid.text.strip(),
    tid.text.strip()
])
channels.append(channel)

Since you're applying text.strip() to each element, you can simplify this using a list comprehension:

elements = [name, genre, bouquets, encryption, sid, nid, tid]
channel = "\t".join(
    x.text.strip() for x in elements
)
channels.append(channel)

Upvotes: 2

Related Questions