473,659 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

make a string a list

or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.

Thanks,
Nikhil
Jun 27 '08 #1
7 2505
On May 29, 4:30 pm, Nikhil <mnik...@gmail. comwrote:
or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.
Hmmm. Can you state your problem in a different way?

I find that Python excels at string handling and I've never had to
write a pure character scanner like you are suggesting. Not to say
that you don't need it, I just think that if you give us a small
example of your data and your expected outcome, it would be easier to
help you.
Jun 27 '08 #2
On May 29, 2:30 pm, Nikhil <mnik...@gmail. comwrote:
or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.

Thanks,
Nikhil
I a little confused by what you are trying to do, but:

Strings are iterable.
>>for c in "hello":
.... print "look, a letter", c
....
look, a letter h
look, a letter e
look, a letter l
look, a letter l
look, a letter o

And indexable:
>>"hello"[0]
'h'

And converting to a list is done like this:
>>list("hello ")
['h', 'e', 'l', 'l', 'o']

They also have some pretty nifty methods:
>>"hello\r\nwor ld\r\nlets\r\ns plit\r\n".split ("\r\n")
['hello', 'world', 'lets', 'split', '']
>>"hello world".index('w orld')
6
>>"hello world".index('l ')
2
>>"hello world".index('l ', 2+1)
3
>>"hello world".index('l ', 3+1)
9
>>"hello world".index('l ', 9+1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found

Hopefully some of that helps.

Matt
Jun 27 '08 #3
Nikhil <mn*****@gmail. comwrites:
or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.
What problem are you trying to solve?

Are you aware that Python file objects are already iterable, yielding
one line of text per iteration?

input_file = open("foo.txt")
for line in input_file:
do_stuff(line)

--
\ "I'd take the awe of understanding over the awe of ignorance |
`\ any day." -- Douglas Adams |
_o__) |
Ben Finney
Jun 27 '08 #4
Nikhil wrote:
or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.
Why dont the ``find`` or ``index`` methods work for you?
http://docs.python.org/lib/string-methods.html

Cheers,
Alan Isaac
Jun 27 '08 #5
On May 29, 11:30 pm, Nikhil <mnik...@gmail. comwrote:
or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.

Thanks,
Nikhil
If you just want to check required value then you can even use Sets or
build your own hash table.
Jun 27 '08 #6
>or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.
They already are. They are quite like lists in many ways:
>>s = 'abcdefg'
for c in s: print c
....
a
b
c
d
e
f
g
>>s[3]
'd'
>>s = "foo\r\n"
s.find("\r" )
3
>>s.replace("\r ", "").replace("\n ", "")
'foo'
>>>
** Posted from http://www.teranews.com **
Jun 27 '08 #7
Lie
On May 30, 4:30*am, Nikhil <mnik...@gmail. comwrote:
or a string iterable ? How can I do that. I have lots of '\r\n'
characters in the string which I think can be easier if it were made
into a list and I can easily see if the required value (its a numeral)
is present in it or not after some position or after some characters'
position.

Thanks,
Nikhil
Isn't it already iterable? And combined with str.split(), it could be
line iterable too.
Jun 27 '08 #8

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

Similar topics

7
6275
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments passed to the function are strings or must be (automaticly converted to a string e.g. the number 10 should become the string "10". My problem is that I can only find samples and description of printf() like functions where the optional arguments and...
10
1625
by: Daniel | last post by:
how to make two references to one string that stay refered to the same string reguardless of the changing value in the string?
6
4868
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
4
2768
by: Kyote | last post by:
I'm trying to persist a list of filenames. I've made a custom collection and a FileName class: 'Class to hold file name information Public Class FileNames Public fullName As String Public fileName As String Public fileExtention As String Public filePath As String Public newName As String
2
6892
by: Øyvind Isaksen | last post by:
Mabye this is question is very simple, but I need to make a string-array (in a variable) with the values from a checkbox-list. THANKS :)
2
1150
by: james | last post by:
I recieve a business object and need to create a form to edit each property. This object is huge (like over 100 properties), and I dont want to make 100 label/textboxes by hand. Does anyone know how to easily generate the form? By properties I mean the business object looks like: public class BusinessObject { public string prop1; public string prop2;
1
7287
by: xahlee | last post by:
Elisp Tutorial: Make Google Earth Xah Lee, 2006-12 This page shows a example of writing a emacs lisp function that creates a Google Earth file, and creates a link to the file, as well a link to Google Map. If you don't know elisp, first take a gander at Elisp Basics. I often write travelogs on my website. If i traveled to Las Vegas, then
19
2196
by: active | last post by:
I'm using a ComboBox to display objects of a class I've defined, say CQQ. Works great except somehow I occasionally set an Item to a String object instead of an object of type CQQ. It looks like Text replaces an item or something like that. This results in a runtime error at which time I learn that the item that should be CQQ is a String.
1
1178
by: nuffnough | last post by:
I have defined two classes with one common field (called code) and several different fields. In class A there is only one instance of any given code as all items are individual. In class B, there may be none, one or many instances of each code, as there can be any number of Bs referring to a single A. I need to make a list of Bs, remove dupes, and then create a list of As that have Bs.
2
1079
by: Gustaf | last post by:
I had VS (2008) telling me my class isn't enumerable when trying to use a foreach loop. This happens quite often, and every time I Google around for an hour trying to find a solution that applies to my case. It needs to be understandable and easy to implement. It needs to work with List<Tcollections. It should be the best practice in .NET 3.5. Can anyone point me once and for all to the right solution? The best I can find right now is this: ...
0
8851
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
8746
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...
1
8525
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7356
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...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
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
4175
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
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
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

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.