0

I have a following nested list (All are in one list: One master list (Master) contains two list both of them contain by one list)

Master = \
["E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin [u'BGD_4_new_district', u'BGD_3_old_district', u'BGD_2_division', u'BGD_1_all', u'BGD_5_Upazilla', u'BGD_4_old_district', u'BGD_6_Union_court', u'BGD_6_Union', u'BD_exposed_coastal_area', u'BD_drought', u'BGD_1_River', u'BGD_1_River_detail', u'BD_international_bnd', u'BGD_1_River_1', u'BGD_7_Mauza', u'test', u'BGD_5_UpazillaAnno', u'BGD_4_new_districtAnno', u'BGD_4_new_districtAnno2']", 
 "E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BCAS_BD_Infrastructure    [u'BD_Health_Infrastructures_1', u'BD_Railway_Establishments_1', u'BGD_roads_1']"]

Now I want to make a list (as windows path) whose one element as below:(for the first list)

"E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\u'BGD_4_new_district"

again the included 'u' character for unicode and "'" after it also be removed so finally it be: In a elements in a List named "My_Path" are

"E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_4_new_district" 
"E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_3_old_district"
..........
..........
"E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BCAS_BD_Infrastructur\\BD_Health_Infrastructures_1"

@ Martijn Pieters My arcpy code is

import arcpy,os
p=[]
D=[]
admin="E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb"
arcpy.env.workspace =admin
datasetList = arcpy.ListDatasets("*", "Feature")
for dataset in datasetList:
d=str(dataset)
D.append(d)
dList = arcpy.ListTables() + arcpy.ListFeatureClasses() + arcpy.ListRasters()
for dataset in arcpy.ListDatasets():
    arcpy.env.workspace=os.path.join(admin,dataset)
    dList+=arcpy.ListDatasets()+arcpy.ListFeatureClasses()
    p.append(str(arcpy.env.workspace)+str(arcpy.ListFeatureClasses()))##--this makes list
Learner
  • 5,192
  • 1
  • 24
  • 36
  • 1
    You have one list with two strings. They are not nested lists. Part of the elements *look* like lists, but they are still just strings. You called `str()` on existing lists with unicode values. – Martijn Pieters Jun 03 '13 at 08:23
  • 1
    Show us the code that build this list in the first place? I can help you fix these strings, but it'd be much, much easier and better if you didn't build these values in the first place. – Martijn Pieters Jun 03 '13 at 08:24
  • @ Martijn Pieters i m usin arcpy library – Learner Jun 03 '13 at 08:54
  • arcpy does not produce output like this unless you call `str()` on something (or use string formatting). What is the code that produces `Master`? – Martijn Pieters Jun 03 '13 at 09:03
  • @ Martijn Pieters yes boss i used str() formatting – Learner Jun 03 '13 at 09:08
  • Then please show that bit of code and I can help you avoid the EOF you are seeing with my attempt to repair the damage. Edit your question, add the lines of code that create the `Master` list. – Martijn Pieters Jun 03 '13 at 09:10

1 Answers1

2

You have two strings with python lists embedded in them. They are not actual lists.

You can go back to making these into lists with unicode values with the ast.literal_eval() function. That feels like we are fixing a problem that was caused by other code, the better fix would be to not build these values like these in the first place.

from ast import literal_eval
import os.path

output = []
for entry in Master:
    base, lst = entry.split(None, 1)
    for name in literal_eval(lst):
        output.append(os.path.join(base, name))

This produces proper unicode paths (a good idea on Windows):

>>> output
[u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_4_new_district', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_3_old_district', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_2_division', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_1_all', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_5_Upazilla', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_4_old_district', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_6_Union_court', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_6_Union', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BD_exposed_coastal_area', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BD_drought', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_1_River', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_1_River_detail', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BD_international_bnd', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_1_River_1', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_7_Mauza', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\test', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_5_UpazillaAnno', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_4_new_districtAnno', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin\\BGD_4_new_districtAnno2', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BCAS_BD_Infrastructure\\BD_Health_Infrastructures_1', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BCAS_BD_Infrastructure\\BD_Railway_Establishments_1', u'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BCAS_BD_Infrastructure\\BGD_roads_1']
>>> print output[0]
E:\GIS_DOCUMENT\BCAS_Map\BCAS_All.gdb\BD_Admin\BGD_4_new_district

If you can, you want to change the original code to produce a dictionary instead, one that uses the base as keys, and a list of names as the value:

{'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BCAS_BD_Infrastructure': [u'BD_Health_Infrastructures_1',
                                                                      u'BD_Railway_Establishments_1',
                                                                      u'BGD_roads_1'],
 'E:\\GIS_DOCUMENT\\BCAS_Map\\BCAS_All.gdb\\BD_Admin': [u'BGD_4_new_district',
                                                        u'BGD_3_old_district',
                                                        u'BGD_2_division',
                                                        u'BGD_1_all',
                                                        u'BGD_5_Upazilla',
                                                        u'BGD_4_old_district',
                                                        u'BGD_6_Union_court',
                                                        u'BGD_6_Union',
                                                        u'BD_exposed_coastal_area',
                                                        u'BD_drought',
                                                        u'BGD_1_River',
                                                        u'BGD_1_River_detail',
                                                        u'BD_international_bnd',
                                                        u'BGD_1_River_1',
                                                        u'BGD_7_Mauza',
                                                        u'test',
                                                        u'BGD_5_UpazillaAnno',
                                                        u'BGD_4_new_districtAnno',
                                                        u'BGD_4_new_districtAnno2']}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343