473,791 Members | 2,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to parse a string completely into a list

I want to take a long alpha-numeric string with \n and white-space and
place ALL elements of the string (even individual parts of a long
white-space) into separate list elements. The most common way I've
seen this performed is with the split() function, however I don't
believe that it has the power to do what I am looking for.
Any suggestions?
thanks
Sep 25 '08 #1
7 1622
On Wed, Sep 24, 2008 at 8:30 PM, <jo*******@colo rado.eduwrote:
I want to take a long alpha-numeric string with \n and white-space and
place ALL elements of the string (even individual parts of a long
white-space) into separate list elements. The most common way I've
seen this performed is with the split() function, however I don't
believe that it has the power to do what I am looking for.
Any suggestions?
thanks
--
http://mail.python.org/mailman/listinfo/python-list
Could you please define exactly what you mean by "elements" of a string?

If you mean characters, then just use list():
>>list(" \n \t abc")
[' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']

Regards,
Chris

--
Follow the path of the Iguana...
http://rebertia.com
Sep 25 '08 #2
On Sep 24, 9:44*pm, "Chris Rebert" <c...@rebertia. comwrote:
On Wed, Sep 24, 2008 at 8:30 PM, *<john.f...@col orado.eduwrote:
I want to take a long alpha-numeric string with \n and white-space and
place ALL elements of the string (even individual parts of a long
white-space) into separate list elements. The most common way I've
seen this performed is with the split() function, however I don't
believe that it has the power to do what I am looking for.
Any suggestions?
thanks
--
http://mail.python.org/mailman/listinfo/python-list

Could you please define exactly what you mean by "elements" of a string?

If you mean characters, then just use list():>>list(" *\n \t abc")

[' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']

Regards,
Chris

--
Follow the path of the Iguana...http://rebertia.com
Worked like a charm.
kudos!
Sep 25 '08 #3
jo*******@color ado.edu wrote:
On Sep 24, 9:44 pm, "Chris Rebert" <c...@rebertia. comwrote:
>On Wed, Sep 24, 2008 at 8:30 PM, <john.f...@colo rado.eduwrote:
>>I want to take a long alpha-numeric string with \n and white-space and
place ALL elements of the string (even individual parts of a long
white-space) into separate list elements. The most common way I've
seen this performed is with the split() function, however I don't
believe that it has the power to do what I am looking for.
Any suggestions?
thanks
Could you please define exactly what you mean by "elements" of a string?

If you mean characters, then just use list():>>list(" \n \t abc")

[' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']

Regards,
Chris

Worked like a charm.
kudos!
Why do you need to convert it to a list? Strings are sequences, so you
can do things like slice them or iterate through them by character:
>>for character in "foo":
.... print character
....
f
o
o
>>>
--
Sep 25 '08 #4
On Sep 24, 10:12*pm, Matt Nordhoff <mnordh...@matt nordhoff.comwro te:
john.f...@color ado.edu wrote:
On Sep 24, 9:44 pm, "Chris Rebert" <c...@rebertia. comwrote:
On Wed, Sep 24, 2008 at 8:30 PM, *<john.f...@col orado.eduwrote:
I want to take a long alpha-numeric string with \n and white-space and
place ALL elements of the string (even individual parts of a long
white-space) into separate list elements. The most common way I've
seen this performed is with the split() function, however I don't
believe that it has the power to do what I am looking for.
Any suggestions?
thanks
Could you please define exactly what you mean by "elements" of a string?
If you mean characters, then just use list():>>list(" *\n \t abc")
[' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']
Regards,
Chris
Worked like a charm.
kudos!

Why do you need to convert it to a list? Strings are sequences, so you
can do things like slice them or iterate through them by character:
>for character in "foo":

... * * print character
...
f
o
o

--
The string draws a map that I then want to be able to traverse
through. If I can count through the individual characters of a list I
can create an x-y coordinate plane for navigation.
Sep 25 '08 #5
jo*******@color ado.edu wrote:
>
The string draws a map that I then want to be able to traverse
through. If I can count through the individual characters of a list I
can create an x-y coordinate plane for navigation.
Well, the point Matt was making is that traversing through a list and
traversing through a string are the same.

# Given this:
s = 'abcde'
l = ['a','b','c','d' ,'e']

# These are identical:
for ch in s:
pass
for ch in l:
pass

# And these are identical:
print s[3]
print l[3]

Slicing is identical. Subsetting is identical. The only difference is
that I can change an element of the list.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Sep 25 '08 #6
jo*******@color ado.edu a écrit :
I want to take a long alpha-numeric string with \n and white-space and
place ALL elements of the string (even individual parts of a long
white-space) into separate list elements. The most common way I've
seen this performed is with the split() function, however I don't
believe that it has the power to do what I am looking for.
Any suggestions?
Did you try passing your string to the list() type ?

Python 2.5.1 (r251:54863, Mar 7 2008, 03:41:45)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>s = """I want to take a long alpha-numeric string with \n and
white-space and
.... place ALL elements of the string (even individual parts of a long
.... white-space) into separate list elements. The most common way I've
.... seen this performed is with the split() function, however I don't
.... believe that it has the power to do what I am looking for.
.... Any suggestions?
.... thanks
.... """
>>>
s
"I want to take a long alpha-numeric string with \n and white-space
and\nplace ALL elements of the string (even individual parts of a
long\nwhite-space) into separate list elements. The most common way
I've\nseen this performed is with the split() function, however I
don't\nbelieve that it has the power to do what I am looking for.\nAny
suggestions?\nt hanks\n"
>>list(s)
['I', ' ', 'w', 'a', 'n', 't', ' ', 't', 'o', ' ', 't', 'a', 'k', 'e', '
', 'a', ' ', 'l', 'o', 'n', 'g', ' ', 'a', 'l', 'p', 'h', 'a', '-', 'n',
'u', 'm', 'e', 'r', 'i', 'c', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ',
'w', 'i', 't', 'h', ' ', '\n', ' ', 'a', 'n', 'd', ' ', 'w', 'h', 'i',
't', 'e', '-', 's', 'p', 'a', 'c', 'e', ' ', 'a', 'n', 'd', '\n', 'p',
'l', 'a', 'c', 'e', ' ', 'A', 'L', 'L', ' ', 'e', 'l', 'e', 'm', 'e',
'n', 't', 's', ' ', 'o', 'f', ' ', 't', 'h', 'e', ' ', 's', 't', 'r',
'i', 'n', 'g', ' ', '(', 'e', 'v', 'e', 'n', ' ', 'i', 'n', 'd', 'i',
'v', 'i', 'd', 'u', 'a', 'l', ' ', 'p', 'a', 'r', 't', 's', ' ', 'o',
'f', ' ', 'a', ' ', 'l', 'o', 'n', 'g', '\n', 'w', 'h', 'i', 't', 'e',
'-', 's', 'p', 'a', 'c', 'e', ')', ' ', 'i', 'n', 't', 'o', ' ', 's',
'e', 'p', 'a', 'r', 'a', 't', 'e', ' ', 'l', 'i', 's', 't', ' ', 'e',
'l', 'e', 'm', 'e', 'n', 't', 's', '.', ' ', 'T', 'h', 'e', ' ', 'm',
'o', 's', 't', ' ', 'c', 'o', 'm', 'm', 'o', 'n', ' ', 'w', 'a', 'y', '
', 'I', "'", 'v', 'e', '\n', 's', 'e', 'e', 'n', ' ', 't', 'h', 'i',
's', ' ', 'p', 'e', 'r', 'f', 'o', 'r', 'm', 'e', 'd', ' ', 'i', 's', '
', 'w', 'i', 't', 'h', ' ', 't', 'h', 'e', ' ', 's', 'p', 'l', 'i', 't',
'(', ')', ' ', 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n', ',', ' ', 'h',
'o', 'w', 'e', 'v', 'e', 'r', ' ', 'I', ' ', 'd', 'o', 'n', "'", 't',
'\n', 'b', 'e', 'l', 'i', 'e', 'v', 'e', ' ', 't', 'h', 'a', 't', ' ',
'i', 't', ' ', 'h', 'a', 's', ' ', 't', 'h', 'e', ' ', 'p', 'o', 'w',
'e', 'r', ' ', 't', 'o', ' ', 'd', 'o', ' ', 'w', 'h', 'a', 't', ' ',
'I', ' ', 'a', 'm', ' ', 'l', 'o', 'o', 'k', 'i', 'n', 'g', ' ', 'f',
'o', 'r', '.', '\n', 'A', 'n', 'y', ' ', 's', 'u', 'g', 'g', 'e', 's',
't', 'i', 'o', 'n', 's', '?', '\n', 't', 'h', 'a', 'n', 'k', 's', '\n']
>>>

HTH
Sep 25 '08 #7
On Sep 25, 1:51*am, Tino Wildenhain <t...@wildenhai n.dewrote:
john.f...@color ado.edu wrote:
On Sep 24, 10:12 pm, Matt Nordhoff <mnordh...@matt nordhoff.comwro te:
john.f...@color ado.edu wrote:
On Sep 24, 9:44 pm, "Chris Rebert" <c...@rebertia. comwrote:
....
>>Could you please define exactly what you mean by "elements" of a string?
If you mean characters, then just use list():>>list(" *\n \t abc")
[' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']
Regards,
Chris
Worked like a charm.
kudos!
Why do you need to convert it to a list? Strings are sequences, so you
can do things like slice them or iterate through them by character:
>>>for character in "foo":
... * * print character
...
f
o
o
--
The string draws a map that I then want to be able to traverse
through. If I can count through the individual characters of a list I
can create an x-y coordinate plane for navigation.

You can 'count' (whatever that means) equally in strings as you do in
lists. As said above, they behave exactly the same. Just strings
are imutable - e.g. you can't change individual parts of them.

Tino
--
http://mail.python.org/mailman/listinfo/python-list

*smime.p7s
4KViewDownload
Ahh, but I forgot to mention that I have to mark the path I took in
the string. So using list() and then join() are my best options.

Sep 25 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
22044
by: N | last post by:
Hi, I would like to parse out each value that is seperated by a comma in a field and use that value to join to another table. What would be the easiest way to do so without having to write a function or routine ? EX. Table AAA COL1 COL2
19
20590
by: linzhenhua1205 | last post by:
I want to parse a string like C program parse the command line into argc & argv. I hope don't use the array the allocate a fix memory first, and don't use the memory allocate function like malloc. who can give me some ideas? The following is my program, but it has some problem. I hope someone would correct it. //////////////////////////// //Test_ConvertArg.c ////////////////////////////
14
3692
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style of date formatting (RFC 822). I have been using a variation of RFC 1123 (just change the time zone to an offset, i.e. "-0800"). It seems to be writing okay, but it's failing to parse. I've tried changing the regional & language settings in my...
0
1374
by: KarenS | last post by:
I am using Microsoft Developer Environment 2003 Version 7.1.3088 and .NET Framework version 1.1.4322 SP1. From my web-service I get one row of data and return it to my client. From my client I want to be able to parse apart the Data in the returned XML string by field name. (<group_name>, <city>, <state?, etc.) Thank-you. More details follow: (The Data set is sent to the client from the web-service with .GetXML, see
29
2909
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
5
64652
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
1
64204
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
2
3565
by: shapper | last post by:
Hello, I have the following class ( Level is just a simple enumeration ): public class Theme { public Subject Subject { get; set; } public List<LevelLevels { get; set; } public string Note { get; set; } }
2
18635
by: nithin.papdeja | last post by:
Hi all, I have to parse a .txt file and retrieve only "Name" from second column and store it in a list My text file is of this format: Bit Name Description -1 Lucky groupa -1 Roy groupb
0
10426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.