473,387 Members | 1,569 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,387 software developers and data experts.

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.urlretrieve(website, 'getfile.txt')

filename = "getfile.txt"
input = open(filename, 'r')
reg1 = re.compile('href=".*"')
reg3 = re.compile('".*?"')
reg4 = re.compile('http')
Line = input.readline()

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

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

I get this:

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

instead of this:

["http://www.slugnuts.com/index.html]",
"http://www.slugnuts.com/movies.html]",
"http://www.slugnuts.com/ramblings.html]",
"http://www.slugnuts.com/sluggies.html]",
"http://www.slugnuts.com/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 992
On Wed, 07 Sep 2005 16:34:25 GMT, colonel <th******@camelrichard.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.urlretrieve(website, 'getfile.txt')

filename = "getfile.txt"
input = open(filename, 'r')
reg1 = re.compile('href=".*"')
reg3 = re.compile('".*?"')
reg4 = re.compile('http')
Line = input.readline()

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

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

I get this:

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

instead of this:

["http://www.slugnuts.com/index.html]",
"http://www.slugnuts.com/movies.html]",
"http://www.slugnuts.com/ramblings.html]",
"http://www.slugnuts.com/sluggies.html]",
"http://www.slugnuts.com/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.append(cleanlink)
else:
cleanlink2 = str(website) + "/" + str(cleanlink)

to

fullink = reg4.search(cleanlink[0])
if fullink:
linkArray.append(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******@camelrichard.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.urlretrieve(website, 'getfile.txt')

filename = "getfile.txt"
input = open(filename, 'r')
reg1 = re.compile('href=".*"')
reg3 = re.compile('".*?"')
reg4 = re.compile('http')
Line = input.readline()

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

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

I get this:

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

instead of this:

["http://www.slugnuts.com/index.html]",
"http://www.slugnuts.com/movies.html]",
"http://www.slugnuts.com/ramblings.html]",
"http://www.slugnuts.com/sluggies.html]",
"http://www.slugnuts.com/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.append(cleanlink)
else:
cleanlink2 = str(website) + "/" + str(cleanlink)

to

fullink = reg4.search(cleanlink[0])
if fullink:
linkArray.append(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******@camelrichard.org> wrote in message
news:cu********************************@4ax.com...
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.com/['index.html']",
"http://www.slugnuts.com/['movies.html']",
"http://www.slugnuts.com/['ramblings.html']",
"http://www.slugnuts.com/['sluggies.html']",
"http://www.slugnuts.com/['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 anansispaceworks.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
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
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...
1
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...
3
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...
6
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...
9
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...
5
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 &...
34
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. ...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.