Click here to Skip to main content
14,872,121 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I am newbie with Python. I would like to convert string to array.
This is my strin looks like:

Hello
World


my expectation it become ["hello","world"]

What I have tried:

This is my string comes from:

Python
<Country>
         <number no="11" info="date">
              <detail name="a1" class="11a" />
                   <string name="hello" />
                   <string name="world" />
              </detail>
              <detail name="a2" class="1a2" />
                   <string name="hello" />
                   <string name="world" />
              </detail>
         </number>
    <Country>

    XML = ET.parse(File)
    for name in XML.findall('.//detail [@class="11a"]/'):
       detname = name.get('name')
       print(detname)


the output of `detname` is like this:

hello
world

I tried like this:

Python
arr = string.split()
print(arr)


it return this: ['world']
it not include the "hello".
Anyone can help me please. Thank you
Posted
Updated 9-Mar-21 15:32pm
v3
Comments
Richard MacCutchan 9-Mar-21 7:38am
   
Where is the code that creates the string?
Member 14156312 9-Mar-21 21:29pm
   
I updated the question how I got the string
Richard MacCutchan 10-Mar-21 3:21am
   
Well that is totally different issue from your original question. You do not have a single string that contains two words, but two separate strings: "hello" and "world".
Richard MacCutchan 10-Mar-21 3:32am
   
I have just tried to run that code and nothing works, including the fact that the XML is badly formed. Can I suggest you actually test your code and post exactly what does work.

Go to The Python Tutorial — Python 3.7.10 documentation[^] where everything is explained in clear terms.
   
Comments
Maciej Los 9-Mar-21 5:54am
   
5ed!
CPallini 9-Mar-21 7:20am
   
5.
Quote:
This is my strin looks like:
Hello
World

I tried like this:
Python
arr = string.split()
print(arr)


it return this: ['World']
it not include the "hello".


Not true!

Take a look at below examples:
Python
#Example 1
txt = "Hello,\nWord!"
x = txt.split()
print(x) 
#returns: ['Hello,', 'Word!']

#Example 2
txt = "welcome to the jungle"
x = txt.split()
print(x) 
#returns: ['welcome', 'to', 'the', 'jungle']
   
Comments
CPallini 9-Mar-21 7:20am
   
5.
Maciej Los 9-Mar-21 9:03am
   
Thank you, Carlo.
Member 14156312 9-Mar-21 21:33pm
   
I updated the question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900