Array Combining Values

LunchBox

I have an array that looks like this:

guest_list = ['P', 'r', 'o', 'f', '.', ' ', 'P', 'l', 'u', 'm', '\n', 'M', 'i', 's', 's', ' ', 'S', 'c', 'a', 'r', 'l', 'e', 't', '\n', 'C', 'o', 'l', '.', ' ', 'M', 'u', 's', 't', 'a', 'r', 'd', '\n', 'A', 'l', ' ', 'S', 'w', 'e', 'i', 'g', 'a', 'r', 't', '\n', 'R', 'o', 'b', 'o', 'c', 'o', 'p']

What I want is an array that looks like this:

guest_list = ['Prof.Plum', 'Miss Scarlet', 'Col. Mustard', 'Al Sweigart', 'Robocop']

In other words, until '\n' appears, I want all of the string values to be combined into 1 value.

Any suggestions?

Edit #1:

Here is part of my original code:

ogl = open('guests.txt') #open guest list
pyperclip.copy(ogl.read()) #open guest list copy
guest_list = list(pyperclip.paste())
user3483203

Simply use str.join and str.split:

>>> ''.join(x).split('\n')
['Prof. Plum', 'Miss Scarlet', 'Col. Mustard', 'Al Sweigart', 'Robocop']

Since you've updated your question to show how you read in the file, here is what you really should be doing:

with open('guests.txt') as ogl:
    pyperclip.copy(ogl.read())
    guest_list = pyperclip.paste().split('\n')

Or something along those lines, although I'm not sure why you are doing the copy/paste thing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related