<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Modify CSV writer script to define list of columns by field name in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120620#M63089</link>
    <description>&lt;P&gt;in which case, i'll replace commas with semicolons before exporting. Might be the only way around it&lt;/P&gt;</description>
    <pubDate>Sun, 28 Nov 2021 21:44:31 GMT</pubDate>
    <dc:creator>David_Brooks</dc:creator>
    <dc:date>2021-11-28T21:44:31Z</dc:date>
    <item>
      <title>Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120409#M63066</link>
      <description>&lt;P&gt;I have a script that writes a CSV based on fields from a feature class. Currently, the columns are defined by a field list, and a Search cursor loops through each item in the list to write to the CSV.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The list is defined by the positions of each field in the list. However, if the field order ever changes on the Feature Class, this list goes to pot.&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I rewrite it so that I can define fld_list_final as a list of strings that match the actual field names, instead of their positions in the list?&lt;/P&gt;&lt;P&gt;So, for example, instead of the list being fn[5], fn[6] etc, i want to be able to define it as 'TreeRef', 'Species', etc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;   # Create CSV of data for schedule if there are veteran trees
   import csv
   import os
   def tableToCSV(input_tbl, csv_filepath):
       fld_list = arcpy.ListFields(input_tbl)
       fn = [fld.name for fld in fld_list]
       fld_list_final = [fn[3], fn[4], fn[5], fn[6], fn[7], fn[8], fn[38], fn[41], fn[22], fn[23], fn[24], fn[25], fn[26], fn[27], fn[28], fn[29], fn[30], fn[36], fn[40], fn[39], fn[33], fn[37]]
       with open(csv_filepath, 'w', newline='') as csv_file:
           writer = csv.writer(csv_file)
           writer.writerow(fld_list_final)
           with arcpy.da.SearchCursor(input_tbl, fld_list_final) as cursor:
               for row in cursor:
                   writer.writerow(row)
       csv_file.close()
   out_csv = csv_path+"\\"+(os.path.basename(fc_pnt))+".csv"
   tableToCSV(fc_pnt, out_csv)
   arcpy.AddMessage("CSV Generated.")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 10:29:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120409#M63066</guid>
      <dc:creator>David_Brooks</dc:creator>
      <dc:date>2021-11-26T10:29:24Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120411#M63067</link>
      <description>&lt;P&gt;perhaps the suggestion of using a dictionary to alter field order when exporting to csv can be exploited&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-questions/changing-column-order-with-python-export-csv/td-p/18553" target="_blank"&gt;Solved: Changing Column Order with Python Export CSV - Esri Community&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 10:36:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120411#M63067</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-11-26T10:36:52Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120439#M63069</link>
      <description>&lt;P&gt;If you have a set list of field names that you want in your output, you can define those separately, then filter your field list with it. Try this? I don't have a dataset handy to test it on.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Create CSV of data for schedule if there are veteran trees
import csv
import os

def tableToCSV(input_tbl, csv_filepath):
    fld_list = arcpy.ListFields(input_tbl)
    keepers = ['some', 'list', 'of', 'field', 'names']
    fn = [fld.name for fld in fld_list if fld.name in keepers]
    with open(csv_filepath, 'w', newline='') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow(fn)
        with arcpy.da.SearchCursor(input_tbl, fn) as cursor:
            for row in cursor:
                writer.writerow(row)
    csv_file.close()

out_csv = f"{csv_path}\\{os.path.basename(fc_pnt)}.csv"
tableToCSV(fc_pnt, out_csv)
arcpy.AddMessage("CSV Generated.")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 14:37:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120439#M63069</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-11-26T14:37:20Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120492#M63071</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/363906"&gt;@jcarlson&lt;/a&gt;&amp;nbsp;thanks for the re-write, but I get&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;TypeError: list indices must be integers or slices, not str&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;....which i think is because Im not extracting items from a list by index, rather by string values. Any ideas?&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 20:01:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120492#M63071</guid>
      <dc:creator>David_Brooks</dc:creator>
      <dc:date>2021-11-26T20:01:41Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120507#M63072</link>
      <description>&lt;P&gt;What line is that error pointing you to? The change I made shouldn't really matter with respect to list indices.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jcarlson_1-1637963182731.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/28326i97ECC78BC15DA67E/image-size/large?v=v2&amp;amp;px=999" role="button" title="jcarlson_1-1637963182731.png" alt="jcarlson_1-1637963182731.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 21:46:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120507#M63072</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-11-26T21:46:43Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120512#M63073</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/363906"&gt;@jcarlson&lt;/a&gt;&amp;nbsp;i've rewritten, and the csv now exports. however, the order of fields doesn't match the order i've written them in the keepers list&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 22:50:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120512#M63073</guid>
      <dc:creator>David_Brooks</dc:creator>
      <dc:date>2021-11-26T22:50:36Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120513#M63074</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import numpy as np
tbl = r"C:\Arc_projects\Test_29\Test_29.gdb\sq2"
flds = arcpy.ListFields(tbl)
fnames = [f.name for f in flds]
fnames
['OBJECTID', 'Shape', 'ids', 'CENTROID_X', 'CENTROID_Y', 'INSIDE_X', 'INSIDE_Y', 'PART_COUNT', 'PNT_COUNT', 'Sort_', 'Shape_Length', 'Shape_Area', 'Sort2_']

keepers = ['Sort2_', 'PNT_COUNT', 'PART_COUNT']
arr = arcpy.da.TableToNumPyArray(tbl, keepers)

# -- def time ----------------------------------------
def save_txt(a, name="arr.txt", sep=", ", dt_hdr=True):
    """Save a NumPy structured/recarray to text.

    Parameters
    ----------
    a : array
        Input array.
    name : filename
        Output filename and path otherwise save to script folder.
    sep : separator
        Column separator, include a space if needed.
    dt_hdr : boolean
        If True, add dtype names to the header of the file.
    """
    a_names = ", ".join(a.dtype.names)
    hdr = ["", a_names][dt_hdr]  # use "" or names from input array
    s = np.array(a.tolist(), dtype=np.unicode_)
    widths = [max([len(i) for i in s[:, j]])
              for j in range(s.shape[1])]
    frmt = sep.join(["%{}s".format(i) for i in widths])
    np.savetxt(name, a, fmt=frmt, header=hdr, comments="")
    print("\nFile saved...")
# -----------------------------------------------------------

# now save it    
save_txt(arr, name="c:/temp/test.csv", sep=", ", dt_hdr=True)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;results&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Sort2_, PNT_COUNT, PART_COUNT
0, 11.0, 1.0
1,  8.0, 1.0
2,  6.0, 1.0
3,  5.0, 1.0
4, 10.0, 1.0
5, 10.0, 1.0
6,  5.0, 1.0&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Nov 2021 03:34:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120513#M63074</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-11-27T03:34:54Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120514#M63075</link>
      <description>&lt;P&gt;This is what i have so far, but the CSV field order needs sorting to match the "Keepers" list order&lt;/P&gt;&lt;LI-CODE lang="python"&gt;   # Create CSV of data for schedule if there are veteran trees
   import csv
   import os

   def tableToCSV(input_tbl, csv_filepath):
       fld_list = arcpy.ListFields(input_tbl)
       keepers = ['TreeRef','Species','OnOffSite','Height','NumStems','EstDiam','CalcActStemDiam','CrownRadii',
                  'AvgCanHt','F1stBranchHt','F1stBranchDir','LifeStage','SpecialImp','GeneralObs','HealthVit',
                  'StructCon','EstRemCon','BS5837_category','RPA_Radius_m','RPA_m2','TPO','Sort']
       fn = [fld.name for fld in fld_list if fld.name in keepers]
       with open(csv_filepath, 'w', newline='') as csv_file:
           writer = csv.writer(csv_file)
           writer.writerow(fn)
           with arcpy.da.SearchCursor(input_tbl, fn) as cursor:
               for row in cursor:
                   writer.writerow(row)
       csv_file.close()

   out_csv = f"{csv_path}\\{os.path.basename(fc_pnt)}.csv"
   tableToCSV(fc_pnt, out_csv)
   arcpy.AddMessage("CSV Generated.")&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 26 Nov 2021 23:06:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120514#M63075</guid>
      <dc:creator>David_Brooks</dc:creator>
      <dc:date>2021-11-26T23:06:11Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120515#M63076</link>
      <description>&lt;P&gt;tried this code but get the error&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;Traceback (most recent call last):
  File "C:\Users\DAVID\Desktop\BS5837_TreePoint_v1.2.4.py", line 100, in &amp;lt;module&amp;gt;
    np.savetxt(name, a, fmt=frmt, header=hdr, comments="")
  File "C:\Users\DAVID\Desktop\BS5837_TreePoint_v1.2.4.py", line 92, in save_txt
    If True, add dtype names to the header of the file.
NameError: name 'np' is not defined&lt;/SPAN&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Nov 2021 23:14:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120515#M63076</guid>
      <dc:creator>David_Brooks</dc:creator>
      <dc:date>2021-11-26T23:14:30Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120517#M63077</link>
      <description>&lt;P&gt;Ahhh, forgot the old&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;import numpy as np&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;It is assumed in numpy world and oft forgotten since my IDE automatically loads my needed packages &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Nov 2021 01:11:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120517#M63077</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-11-27T01:11:00Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120564#M63078</link>
      <description>&lt;P&gt;that did it. and we have a winner!!! code worked a treat. i'll have to get my head around numpy one day &lt;span class="lia-unicode-emoji" title=":flexed_biceps:"&gt;💪&lt;/span&gt;&lt;/P&gt;&lt;P&gt;thanks&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/215600"&gt;@DanPatterson&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Nov 2021 20:41:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120564#M63078</guid>
      <dc:creator>David_Brooks</dc:creator>
      <dc:date>2021-11-27T20:41:36Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120566#M63079</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/215600"&gt;@DanPatterson&lt;/a&gt;&amp;nbsp;, just found that NULL cells are being replaced by "nan". Is there any way to replace nan with ""? And, oddly, one of the string fields contains a load of empty spaces before the text, in a way that makes the text appear centrally justified in the cell. Any ideas why that's happening?? In fact, all cells containing text are doing this.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Brooks_SummitGeo_0-1638047270553.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/28344i3CDD9643F7515D45/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Brooks_SummitGeo_0-1638047270553.png" alt="Brooks_SummitGeo_0-1638047270553.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Nov 2021 21:16:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120566#M63079</guid>
      <dc:creator>David_Brooks</dc:creator>
      <dc:date>2021-11-27T21:16:30Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120568#M63080</link>
      <description>&lt;P&gt;Good!&amp;nbsp; More numpy lessons&lt;/P&gt;&lt;P&gt;As for empty cells!&amp;nbsp; you need to provide a null value, you can't have "" in a numeric column but you can provide another number like -999&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# -- Reference ****
# https://numpy.org/doc/stable/reference/generated/numpy.savetxt.html
def save_txt(a, name="arr.txt", sep=", ", dt_hdr=True):
    """Save a NumPy structured/recarray to text."""
    a_names = ", ".join(a.dtype.names)
    hdr = ["", a_names][dt_hdr]  # use "" or names from input array
    s = np.array(a.tolist(), dtype=np.unicode_)
    widths = [5, 5]
    frmt = sep.join(["%-{}s".format(i) for i in widths])  # ** note "%-{}s"
    np.savetxt(name, a, fmt=frmt, header=hdr, comments="")  # 
    print("\nFile saved...")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A text sample gdb table with just text columns&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Package, Required_by
atomicwrites, pytest; spyder
chardet, requests; spyder
cloudpickle, spyder; spyder-kernels
diff-match-patch, spyder
intervaltree, spyder
jedi , ipython; python-language-server; spyder
jinja2, jupyterlab; jupyterlab_server; nbconvert; notebook; sphinx; spyder-notebook
keyring, arcgis; spyder
nbconvert, notebook; spyder
nbformat, ipywidgets; nbconvert; notebook; spyder-notebook
notebook, arcgis; jupyterlab; jupyterlab_server; pro_notebook_integration; spyder-notebook; widgetsnbextension
paramiko, spyder
parso, jedi; spyder
pexpect, spyder
pickleshare, ipython; spyder
psutil, spyder
qdarkstyle, spyder; spyder-notebook
qt   , pyqt; sphinx; spyder
qtawesome, spyder
qtconsole, spyder
requests, arcgis; jupyterlab_server; requests-kerberos; requests-oauthlib; requests-toolbelt; requests_ntlm; sphinx; spyder-notebook
rtree, spyder
sphinx, numpydoc; sphinx; spyder
traitlets, ipykernel; ipython; ipywidgets; jupyter_client; jupyter_core; nbconvert; nbformat; notebook; qtconsole; spyder-notebook
watchdog, spyder&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;text, left justified minimum width per column rather than let it pad out&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Nov 2021 00:49:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120568#M63080</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-11-28T00:49:02Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120571#M63081</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/215600"&gt;@DanPatterson&lt;/a&gt;&amp;nbsp; thanks I'll try that out.&lt;/P&gt;&lt;P&gt;Can I not just convert all columns to string in order to remove nan values? Otherwise I'll leave as nan and remove in excel afterwards.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where do you specify to replace empty cells with -9999? Cheers&lt;/P&gt;</description>
      <pubDate>Sun, 28 Nov 2021 06:28:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120571#M63081</guid>
      <dc:creator>David_Brooks</dc:creator>
      <dc:date>2021-11-28T06:28:21Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120574#M63082</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/215600"&gt;@DanPatterson&lt;/a&gt;&amp;nbsp;adding in the '-' does remove blank spaces, but there's still a single space before the start of each string cell, and lots of white space after strings (padding out to match the number of characters in the other rows for that column), see below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;TreeRef, Species, OnOffSite, Height, NumStems, EstDiam, CalcActStemDiam, CrownRadii, AvgCanHt, F1stBranchHt, F1stBranchDir, LifeStage, SpecialImp, GeneralObs, HealthVit, StructCon, EstRemCon, BS5837_category, RPA_Radius_m, RPA_m2, SpecImpBuff, TPO, Sort
T6 , Whitebeam (Swedish), On, 6.0, 1, None, 260.0, 3-3-2-2, 2.0, 2.0, N , SM, Veteran, Located in raised bed along north boundary of the site. Boundary retaining wall  to north. Prolific mistletoe and dieback of crown.                                                , Poor, Fair, &amp;lt;10, U , 3.1, 31.0, 8.5, None, 6 
T7 , Whitebeam (Swedish), On, 6.0, 1, None, 280.0, 3-3-2-2, 2.0, 2.0, N , SM, None   , Located in raised bed along north boundary of the site. Boundary retaining wall  to north. Redundant stake and guard embedded in trunk.                                            , Fair, Fair, 10+, C2, 3.3, 35.0, nan, None, 7 
T8 , Whitebeam (Swedish), On, 6.0, 1, None, 280.0, 3-3-2-2, 2.0, 2.0, E , SM, None   , Located in raised bed along north boundary of the site. Boundary retaining wall  to north. Redundant stake and guard embedded in trunk. Cavity in trunk from 1.5-2.5m.             , Fair, Fair, 10+, C2, 3.3, 35.0, nan, None, 8 
T9 , Whitebeam (Swedish), On, 6.0, 1, None, 250.0, 3-3-2-2, 2.0, 2.0, E , SM, None   , Located in raised bed along north boundary of the site. Boundary retaining wall  to north. Original trunk severely decayed and hollowed; crown regenerating from secondary trunks. , Fair, Poor, &amp;lt;10, U , 3.0, 28.0, nan, None, 9 
T10, Whitebeam (Swedish), On, 6.0, 1, None, 260.0, 3-2-2-2, 2.0, 1.5, NW, SM, None   , Located in raised bed along north boundary of the site. Boundary retaining wall  to north. Cavity in trunk from 1.5-2.5m.                                                          , Fair, Poor, 10+, C2, 3.1, 31.0, nan, None, 10
T11, Whitebeam (Swedish), On, 5.0, 1, None, 190.0, 3-2-2-1, 2.0, 2.0, E , SM, None   , Located in raised bed along north boundary of the site. Boundary retaining wall  to north. Trunk leans to east.                                                                    , Fair, Fair, 10+, C2, 2.3, 16.0, nan, None, 11&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;...is there a way to remove all the excess spaces?&lt;/P&gt;&lt;P&gt;and if i change the width code to match yours&lt;/P&gt;&lt;PRE&gt;widths = [5, 5]&lt;/PRE&gt;&lt;P&gt;...then i get the following error;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;Traceback (most recent call last):
  File "C:\Users\DAVID\Desktop\BS5837_TreePoint_v1.2.4.py", line 174, in &amp;lt;module&amp;gt;
    #          for j in range(s.shape[1])]
  File "C:\Users\DAVID\Desktop\BS5837_TreePoint_v1.2.4.py", line 170, in save_txt
    a_names = ", ".join(a.dtype.names)
  File "&amp;lt;__array_function__ internals&amp;gt;", line 6, in savetxt
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\numpy\lib\npyio.py", line 1413, in savetxt
    raise error
ValueError: fmt has wrong number of % formats:  %-5s, %-5s&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Nov 2021 09:52:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120574#M63082</guid>
      <dc:creator>David_Brooks</dc:creator>
      <dc:date>2021-11-28T09:52:57Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120576#M63083</link>
      <description>&lt;P&gt;widths = [5, 5]&lt;BR /&gt;frmt = sep.join(["%-{}s".format(i) for i in widths]) # ** note "%-{}s"&lt;/P&gt;&lt;P&gt;5 is the minimum number of spaces, some of your columns have 2 characters, so reduce it&lt;/P&gt;&lt;P&gt;widths = [5, 5]&lt;/P&gt;&lt;P&gt;is for 2 fields&lt;/P&gt;&lt;P&gt;Now, lets go back to the original which formatted each column to the maximum entry in each column.&amp;nbsp; We could change it to some other value, or specify the minimum knowing that entries won't be truncated.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#  changed 'max' to 'min'
   s = np.array(a.tolist(), dtype=np.unicode_)
    widths = [min([len(i) for i in s[:, j]])
              for j in range(s.shape[1])]
    frmt = sep.join(["%{}s".format(i) for i in widths])&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 28 Nov 2021 10:22:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120576#M63083</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-11-28T10:22:42Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120578#M63084</link>
      <description>&lt;P&gt;Blog coming, bear with me, I have a number of helper functions and I will be showing how to bring in featureclass tables and standalone table replacing the horrible null with appropriate null values.&lt;/P&gt;&lt;P&gt;The originals are part of&amp;nbsp;&lt;A href="https://community.esri.com/t5/python-blog/table-tools-for-pro/ba-p/904042" target="_blank"&gt;Table Tools for Pro - Esri Community&lt;/A&gt;&lt;/P&gt;&lt;P&gt;which I am currently updating to introduce new functionality and flexibility&lt;/P&gt;</description>
      <pubDate>Sun, 28 Nov 2021 10:30:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120578#M63084</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-11-28T10:30:23Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120614#M63087</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/215600"&gt;@DanPatterson&lt;/a&gt;&amp;nbsp;thanks for your time. changing the value to min and also removing the blanks spaces in the sep="," variables fixed my issue.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think the only last issue I have is that if a field contains any commas, these get utilised as separators, so the data shifts right in the columns. I need to wrap this text in double quotations?&lt;/P&gt;</description>
      <pubDate>Sun, 28 Nov 2021 21:24:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120614#M63087</guid>
      <dc:creator>David_Brooks</dc:creator>
      <dc:date>2021-11-28T21:24:53Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120615#M63088</link>
      <description>&lt;P&gt;You are going to have that issue if you are converting to csv, period.&amp;nbsp; There are many threads on this on the web and they usually entail editing your source files&lt;/P&gt;&lt;P&gt;&lt;A href="https://en.wikipedia.org/wiki/Comma-separated_values" target="_blank"&gt;Comma-separated values - Wikipedia&lt;/A&gt;&lt;/P&gt;&lt;P&gt;compare&lt;/P&gt;&lt;P&gt;1997,Ford,E350,"Super, luxurious truck"&lt;/P&gt;&lt;P&gt;1997,Ford,E350,"Super, ""luxurious"" truck"&lt;/P&gt;&lt;P&gt;what do you think would happen?&lt;/P&gt;&lt;P&gt;My solution? removing the commas is generally easiest if working with existing data&lt;/P&gt;</description>
      <pubDate>Sun, 28 Nov 2021 21:21:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120615#M63088</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-11-28T21:21:11Z</dc:date>
    </item>
    <item>
      <title>Re: Modify CSV writer script to define list of columns by field name</title>
      <link>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120620#M63089</link>
      <description>&lt;P&gt;in which case, i'll replace commas with semicolons before exporting. Might be the only way around it&lt;/P&gt;</description>
      <pubDate>Sun, 28 Nov 2021 21:44:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/modify-csv-writer-script-to-define-list-of-columns/m-p/1120620#M63089</guid>
      <dc:creator>David_Brooks</dc:creator>
      <dc:date>2021-11-28T21:44:31Z</dc:date>
    </item>
  </channel>
</rss>

