473,569 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about concatenation error

I am new to python and I am confused as to why when I try to
concatenate 3 strings, it isn't working properly.

Here is the code:

------------------------------------------------------------------------------------------
import string
import sys
import re
import urllib

linkArray = []
srcArray = []
website = sys.argv[1]

urllib.urlretri eve(website, 'getfile.txt')

filename = "getfile.tx t"
input = open(filename, 'r')
reg1 = re.compile('hre f=".*"')
reg3 = re.compile('".* ?"')
reg4 = re.compile('htt p')
Line = input.readline( )

while Line:
searchstring1 = reg1.search(Lin e)
if searchstring1:
rawlink = searchstring1.g roup()
link = reg3.search(raw link).group()
link2 = link.split('"')
cleanlink = link2[1:2]
fullink = reg4.search(str (cleanlink))
if fullink:
linkArray.appen d(cleanlink)
else:
cleanlink2 = str(website) + "/" + str(cleanlink)
linkArray.appen d(cleanlink2)
Line = input.readline( )

print linkArray
-----------------------------------------------------------------------------------------------

I get this:

["http://www.slugnuts.co m/['index.html']",
"http://www.slugnuts.co m/['movies.html']",
"http://www.slugnuts.co m/['ramblings.html ']",
"http://www.slugnuts.co m/['sluggies.html']",
"http://www.slugnuts.co m/['movies.html']"]

instead of this:

["http://www.slugnuts.co m/index.html]",
"http://www.slugnuts.co m/movies.html]",
"http://www.slugnuts.co m/ramblings.html]",
"http://www.slugnuts.co m/sluggies.html]",
"http://www.slugnuts.co m/movies.html]"]

The concatenation isn't working the way I expected it to. I suspect
that I am screwing up by mixing types, but I can't see where...

I would appreciate any advice or pointers.

Thanks.
Sep 7 '05 #1
4 993
On Wed, 07 Sep 2005 16:34:25 GMT, colonel <th******@camel richard.org>
wrote:
I am new to python and I am confused as to why when I try to
concatenate 3 strings, it isn't working properly.

Here is the code:

------------------------------------------------------------------------------------------
import string
import sys
import re
import urllib

linkArray = []
srcArray = []
website = sys.argv[1]

urllib.urlretr ieve(website, 'getfile.txt')

filename = "getfile.tx t"
input = open(filename, 'r')
reg1 = re.compile('hre f=".*"')
reg3 = re.compile('".* ?"')
reg4 = re.compile('htt p')
Line = input.readline( )

while Line:
searchstring1 = reg1.search(Lin e)
if searchstring1:
rawlink = searchstring1.g roup()
link = reg3.search(raw link).group()
link2 = link.split('"')
cleanlink = link2[1:2]
fullink = reg4.search(str (cleanlink))
if fullink:
linkArray.appen d(cleanlink)
else:
cleanlink2 = str(website) + "/" + str(cleanlink)
linkArray.appen d(cleanlink2)
Line = input.readline( )

print linkArray
-----------------------------------------------------------------------------------------------

I get this:

["http://www.slugnuts.co m/['index.html']",
"http://www.slugnuts.co m/['movies.html']",
"http://www.slugnuts.co m/['ramblings.html ']",
"http://www.slugnuts.co m/['sluggies.html']",
"http://www.slugnuts.co m/['movies.html']"]

instead of this:

["http://www.slugnuts.co m/index.html]",
"http://www.slugnuts.co m/movies.html]",
"http://www.slugnuts.co m/ramblings.html]",
"http://www.slugnuts.co m/sluggies.html]",
"http://www.slugnuts.co m/movies.html]"]

The concatenation isn't working the way I expected it to. I suspect
that I am screwing up by mixing types, but I can't see where...

I would appreciate any advice or pointers.

Thanks.

Okay. It works if I change:

fullink = reg4.search(str (cleanlink))
if fullink:
linkArray.appen d(cleanlink)
else:
cleanlink2 = str(website) + "/" + str(cleanlink)

to

fullink = reg4.search(cle anlink[0])
if fullink:
linkArray.appen d(cleanlink[0])
else:
cleanlink2 = str(website) + "/" + cleanlink[0]
so can anyone tell me why "cleanlink" gets coverted to a list? Is it
during the slicing?
Thanks.
Sep 7 '05 #2
colonel wrote:
On Wed, 07 Sep 2005 16:34:25 GMT, colonel <th******@camel richard.org>
wrote:

I am new to python and I am confused as to why when I try to
concatenate 3 strings, it isn't working properly.

Here is the code:

------------------------------------------------------------------------------------------
import string
import sys
import re
import urllib

linkArray = []
srcArray = []
website = sys.argv[1]

urllib.urlret rieve(website, 'getfile.txt')

filename = "getfile.tx t"
input = open(filename, 'r')
reg1 = re.compile('hre f=".*"')
reg3 = re.compile('".* ?"')
reg4 = re.compile('htt p')
Line = input.readline( )

while Line:
searchstring1 = reg1.search(Lin e)
if searchstring1:
rawlink = searchstring1.g roup()
link = reg3.search(raw link).group()
link2 = link.split('"')
cleanlink = link2[1:2]
fullink = reg4.search(str (cleanlink))
if fullink:
linkArray.appen d(cleanlink)
else:
cleanlink2 = str(website) + "/" + str(cleanlink)
linkArray.appen d(cleanlink2)
Line = input.readline( )

print linkArray
-----------------------------------------------------------------------------------------------

I get this:

["http://www.slugnuts.co m/['index.html']",
"http://www.slugnuts.co m/['movies.html']",
"http://www.slugnuts.co m/['ramblings.html ']",
"http://www.slugnuts.co m/['sluggies.html']",
"http://www.slugnuts.co m/['movies.html']"]

instead of this:

["http://www.slugnuts.co m/index.html]",
"http://www.slugnuts.co m/movies.html]",
"http://www.slugnuts.co m/ramblings.html]",
"http://www.slugnuts.co m/sluggies.html]",
"http://www.slugnuts.co m/movies.html]"]

The concatenation isn't working the way I expected it to. I suspect
that I am screwing up by mixing types, but I can't see where...

I would appreciate any advice or pointers.

Thanks.


Okay. It works if I change:

fullink = reg4.search(str (cleanlink))
if fullink:
linkArray.appen d(cleanlink)
else:
cleanlink2 = str(website) + "/" + str(cleanlink)

to

fullink = reg4.search(cle anlink[0])
if fullink:
linkArray.appen d(cleanlink[0])
else:
cleanlink2 = str(website) + "/" + cleanlink[0]
so can anyone tell me why "cleanlink" gets coverted to a list? Is it
during the slicing?
Thanks.


The statement

cleanlink = link2[1:2]

results in a list of one element. If you want to accesss element one
(the second in the list) then use

cleanlink = link2[1]

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Sep 7 '05 #3

"colonel" <th******@camel richard.org> wrote in message
news:cu******** *************** *********@4ax.c om...
so can anyone tell me why "cleanlink" gets coverted to a list?
Is it during the slicing?


Steve answered for you, but for next time, you could find out faster by
either using the all-purpose debuging tool known as 'print' or, with
Python, the handy-dandy interactive window:
[1,2,3][1:2]

[2]

Terry J. Reedy

Sep 7 '05 #4
On Wednesday 07 September 2005 11:34 am, colonel wrote:
I am new to python and I am confused as to why when I try to
concatenate 3 strings, it isn't working properly.

Here is the code:
I'm not taking the time to really study it, but at first
glance, the code looks like it's probably much more
complicated than it needs to be.
["http://www.slugnuts.co m/['index.html']",
"http://www.slugnuts.co m/['movies.html']",
"http://www.slugnuts.co m/['ramblings.html ']",
"http://www.slugnuts.co m/['sluggies.html']",
"http://www.slugnuts.co m/['movies.html']"]


The tail end of that is the string representation of
a list containing one string, not of that string. I
suspect you needed to use ''.join() somewhere. Or,
you could, in principle have indexed the list, since
you only want one member of it, e.g.:
['index.html'][0]

'index.html'

--
Terry Hancock ( hancock at anansispacework s.com )
Anansi Spaceworks http://www.anansispaceworks.com

Sep 7 '05 #5

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

Similar topics

5
3628
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
1
14131
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of uninitialized value in concatenation (.) at register.pl line 38, <STDIN> line 10." The PERL code is as follows:
1
1765
by: Dan W. | last post by:
I've been acting as messenger the past few days between the BOOST and Digital Mars peoples, and they can't seem to come to an agreement about the semantics of using ## vs. juxtaposition. The problem arose when I was trying to compile a boost example program with the DM compiler, and the name of a file which was put together by a set of...
3
6436
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too...
6
1244
by: Diffident | last post by:
Dear Bruce, Can you please explain me how compiler would be able to optimize "+" for the below code? The reason why I did not want to use string concatenation was that I wanted it to be a single string object in an effort to reduce memory consumption. But if I use "+" does not that consume more memory due to string's inherent property of...
9
2040
by: Justin M. Keyes | last post by:
Hi, Please read carefully before assuming that this is the same old question about string concatenation in C#! It is well-known that the following concatenation produces multiple immutable String objects for each statement: String a = "a"; a += "b";
5
1161
by: vcinquini | last post by:
I'm doing some maintanence in a application where I've found a lot of code like this: strSql = _ "SELECT u.id_usr, u.cd_usr, p.nm_psoa, u.nr_niv_hier_usr" & _ " FROM " & GlobalDB.TABLE_USERS & _ " u, " & GlobalDB.TABLE_PERSONS & _ " p WHERE u.fl_exc_usr = 0 AND p.id_psoa = u.id_psoa" & _ " ORDER BY p.nm_psoa, u.nr_niv_hier_usr"
34
2626
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ____________ THE OVERVIEW I don't remember where I picked it up, but I remember reading years ago that the simple, obvious Python approach for...
4
1610
by: cjl | last post by:
As a learning exercise, I am trying to write a web-based version of 'drawbot' in PHP. See: http://just.letterror.com/ltrwiki/DrawBot I am interested in hearing ideas about how to approach the user input parsing problem. I would like to allow people to type in simple code and have it executed, but I need to limit the code they can write...
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
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. ...
0
8122
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...
0
7970
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...
1
5513
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...
0
3653
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...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
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
0
937
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...

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.