473,395 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Easy question: More items in a For loop?

I'm trying to write a script that will parse IRC chat logs and color
code them if it finds certain characters. I was able to make this
work with one character, but to make it even more accurate I would
like to use two identifying characters. Here is my code :

import urllib2

response = urllib2.urlopen("http://192.168.1.100:81/%23pi.log")

tuna = response.readlines()[-10:]

for j in tuna:
for e,n in j:
if e,n == "*"," ":
j = "This: " + str.strip(j) + " will
be Pink"
elif e,n == "<","%":
j = "This: " + str.strip(j) + " will
be yellow"
elif e,n == "<","@":
j = "This: " + str.strip(j) + " will
be dark pink"

print(str.strip(j))

Obviously the "for e,n" business doesnt work, but I think it makes for
some decent pseudocode for what I'm trying to accomplish.

Here is some sample tuna:
['[7:55pm] <P0keMy teachings goes back to the last iceage.\r\n',
'[7:55pm] <%Zackahh now it does\r\n', '[7:55pm] <%Zackok\r\n',
'[7:55pm] <P0keOr it is down just for you.\r\n', '[7:55pm] <@FC3>
which one? that -12000 ice age or the one before\r\n', '[7:55pm]
<P0kethe earliest..\r\n', '[7:56pm] <P0keso.. 12000 quite long..\r
\n', '[7:56pm] <@FC3the one created by the meteor then\r\n',
'[7:57pm] <P0kedid not know that.. this is just a new teory I am
folding.\r\n', '[7:57pm] * P0ke test test test\r\n']

Apr 6 '07 #1
1 971
Here is some sample tuna:
['[7:55pm] <P0keMy teachings goes back to the last iceage.\r\n',
'[7:55pm] <%Zackahh now it does\r\n', '[7:55pm] <%Zackok\r\n',
'[7:55pm] <P0keOr it is down just for you.\r\n', '[7:55pm] <@FC3>
which one? that -12000 ice age or the one before\r\n', '[7:55pm]
<P0kethe earliest..\r\n', '[7:56pm] <P0keso.. 12000 quite long..\r
\n', '[7:56pm] <@FC3the one created by the meteor then\r\n',
'[7:57pm] <P0kedid not know that.. this is just a new teory I am
folding.\r\n', '[7:57pm] * P0ke test test test\r\n']
You use the split method:

for j in tuna:
time, text = j.split(' ', 1)
if text.startswith('<%'):
print 'Will be yellow'
elif text.startswith('<@'):
print 'Will be pink'
elif text.startswith('*'):
print 'Will be dark pink'

First each line in tuna is looped through. Then you split the line
into two pieces so that it becomes easier to manage. Then you just
check if the line begins with the searched after pattern. If it does,
you color it appropriately.

--
mvh Björn
Apr 6 '07 #2

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

Similar topics

7
by: Alex Hopson | last post by:
Hi, I've got a multidimensional array and I can loop through it fine but I can't get the values of first parts of the array - ie: foreach ($a as $v1) { foreach ($v1 as $v2) { echo "$v2\n";...
1
by: Fraggle | last post by:
I have a repeater with controls added at run time. the <template> also contains a <asp:textbox that is made visible on some repeater elements. when I come to read the text info out it has...
8
by: LedZep | last post by:
What up everyone, I have to write a program that uses a stack to determine whether a string is a palindrome (a string that is spelled identically backward and forward). The program has to...
3
by: Nikolay Petrov | last post by:
I have a Combo box, binded to a dataset With cmbCompany .DataSource = dsSpecContact .DisplayMember = "Companies.CompanyName" .ValueMember = "Companies.CompanyID" End With What I want is to...
9
by: me | last post by:
Hi All, I am new to Classes and learniing the ropes with VB.NET express Here's my question - say I have a want to manage a list of books. Each book has an Author, Title and ISBN Now, I am...
18
by: Nobody | last post by:
I've been looking for a job for a while now, and have run into this interview question twice now... and have stupidly kind of blown it twice... (although I've gotten better)... time to finally...
10
by: could.net | last post by:
Python dict is a hash table, isn't it? I know that hashtable has the concept of "bucket size" and "min bucket count" stuff, and they should be configurable so I can set them to the proper value...
3
by: bh | last post by:
If I want to loop through the values in a listbox, and get either all items in the box, or only selected items, based on a boolean variable passed into a subroutine, which method would be more...
9
by: news.microsoft.com | last post by:
I am looping through an iteration and I would like to test the next item but if its not the one that I want how do I put it back so that when my foreach continues it is in the next iteration? ...
13
by: Joel Koltner | last post by:
Is there an easy way to get a list comprehension to produce a flat list of, say, for each input argument? E.g., I'd like to do something like: for x in range(4) ] ....and receive
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...

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.