11

I have the following list:

>>> poly
'C:\\04-las_clip_inside_area\\16x16grids_1pp_fsa.shp'
>>> record
1373155

and I wish to create:

'C:\\04-las_clip_inside_area\\16x16grids_1pp_fsa_1373155.txt'

I wish to split in order to get the part "C:\04-las_clip_inside_area\16x16grids_1pp_fsa16x16grids_1pp_fsa".

I have tried this two-code-lines solution:

mylist = [poly.split(".")[0], "_", record, ".txt"]
>>> mylist
['C:\\04-las_clip_inside_area\\16x16grids_1pp_fsa', '_', 1373155, '.txt']

from here, reading the example in Python join, why is it string.join(list) instead of list.join(string)?.

I find this solution to joint, but I get this error message:

>>> mylist.join("")
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
AttributeError: 'list' object has no attribute 'join'

Also if I use:

>>> "".join(mylist)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: sequence item 2: expected string, int found
Community
  • 1
  • 1
Gianni Spear
  • 7,033
  • 22
  • 82
  • 131

4 Answers4

20

Python join: why is it string.join(list) instead of list.join(string)?

So there is

"".join(mylist)

instead of

mylist.join("")

There's your error.

To solve your int/string problem, convert the int to string:

mylist= [poly.split(".")[0],"_",str(record),".txt"]

or write directly:

"{}_{}.txt".format(poly.split(".")[0], record)
Community
  • 1
  • 1
eumiro
  • 207,213
  • 34
  • 299
  • 261
  • Thanks, I don't understand why if i try the example: >>>my_list = ["Hello", "world"] >>> print my_list.join("-") i have this error Traceback (most recent call last): File "", line 1, in AttributeError: 'list' object has no attribute 'join' – Gianni Spear Oct 11 '12 at 13:22
  • @Gianni - because there is no `list.join(string)`. There is `string.join(list)`. You've read that linked discussion, didn't you? – eumiro Oct 11 '12 at 13:31
7
>>> from os import path
>>>
>>> path.splitext(poly)
('C:\\04-las_clip_inside_area\\16x16grids_1pp_fsa', '.shp')
>>>
>>> filename, ext = path.splitext(poly)
>>> "{0}_{1}.txt".format(filename, record)
'C:\\04-las_clip_inside_area\\16x16grids_1pp_fsa_1373155.txt'
Paolo Moretti
  • 54,162
  • 23
  • 101
  • 92
  • (+1) splitext seems to be the way to go here to me -- Although you can import `os.path` directly without importing the rest of `os` – mgilson Oct 11 '12 at 13:18
  • I think this is _the_ way to do what the OP wants. Whenever you want to deal with paths you _must_ take a look at it(it's why it's there!) – Bakuriu Oct 11 '12 at 13:20
  • @paolo: thanks. I don't understand why if i try the example of the link: >>>my_list = ["Hello", "world"] >>> print my_list.join("-") i have this error Traceback (most recent call last): File "", line 1, in AttributeError: 'list' object has no attribute 'join' – Gianni Spear Oct 11 '12 at 13:24
  • 1
    @Gianni -- You want `'-'.join(my_list)`. `join` is a string method, not a list method. This allows `join` to work with any iterable, not just lists. – mgilson Oct 11 '12 at 13:28
  • @Gianni Because `join()` method is in the `string` class. Your second example is fine (`"".join(mylist)`), you just have to convert `record` to string. For example, try `"".join([poly.split(".")[0], "_", str(record), ".txt"])` – Paolo Moretti Oct 11 '12 at 13:29
1
>>> poly = 'C:\\04-las_clip_inside_area\\16x16grids_1pp_fsa.shp'
>>> record = 1373155
>>> "{}_{}.txt".format(poly.rpartition('.')[0], record)
'C:\\04-las_clip_inside_area\\16x16grids_1pp_fsa_1373155.txt'

or if you insist on using join()

>>> "".join([poly.rpartition('.')[0], "_", str(record), ".txt"])
'C:\\04-las_clip_inside_area\\16x16grids_1pp_fsa_1373155.txt'

It's important to use rpartition() (or rsplit()) as otherwise it won't work properly if the path has any other '.''s in it

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0

You need to convert record into a string.

mylist= [poly.split(".")[0],"_",str(record),".txt"]
CadentOrange
  • 3,263
  • 1
  • 34
  • 52