# Reimport DataTable(csv) with python

**URL:** <https://forums.unrealengine.com/t/reimport-datatable-csv-with-python/567768>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [May 27, 2022, 9:53am UTC](https://forums.unrealengine.com/t/reimport-datatable-csv-with-python/567768 "2022-05-27T09:53:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hansyuan\_Nb](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hansyuan_nb/32/561253_2.png) [@Hansyuan\_Nb](https://forums.unrealengine.com/u/Hansyuan_Nb)\
**Post date:** [May 27, 2022, 9:53am UTC](https://forums.unrealengine.com/t/reimport-datatable-csv-with-python/567768/1 "2022-05-27T09:53:53Z")

</div>

Hi, I am trying to use python to reimport a DataTable, but I had a hard time to get it right. I don’t know how to assign my custom struct(from C++) in python properly.

> task = unreal.AssetImportTask()  
> task.filename = fpath  
> task.destination\_path = dpath  
> task.replace\_existing = True  
> task.automated = True
> 
> csv\_factory = unreal.CSVImportFactory()  
> outer = unreal.ScriptStruct()  
> csv\_factory.automated\_import\_settings.import\_row\_struct = unreal.ScriptStruct(outer, ‘NameOfMyStruct’)
> 
> task.options = csv\_factory
> 
> asset\_tools = unreal.AssetToolsHelpers.get\_asset\_tools()  
> asset\_tools.import\_asset\_tasks([task])

The error is:

> LogCSVImportFactory: Error: A Data table row type must be specified in the import settings json file for automated import

The ultimate goal is to use BP to execute the python command and reimport the DataTable.  
Any tips will be helpful, thank you.

---

<div class="post-metadata">

**Author:** ![Hansyuan\_Nb](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hansyuan_nb/32/561253_2.png) [@Hansyuan\_Nb](https://forums.unrealengine.com/u/Hansyuan_Nb)\
**Post date:** [May 27, 2022, 10:13am UTC](https://forums.unrealengine.com/t/reimport-datatable-csv-with-python/567768/2 "2022-05-27T10:13:00Z")

</div>

I haven’t found the answer for python, but there is a BP node “Fill Data Table from CSV File” can read csv and update the DataTable. The result is the same. But I am still looking forward the python answer.

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/3/2/632b41e62982e203d3fa595ab5896bac76714995.png)

---

<div class="post-metadata">

**Author:** ![enrico.lapponi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@enrico.lapponi](https://forums.unrealengine.com/u/enrico.lapponi)\
**Post date:** [May 27, 2022, 3:45pm UTC](https://forums.unrealengine.com/t/reimport-datatable-csv-with-python/567768/3 "2022-05-27T15:45:14Z")

</div>

I’m doing something similar, but with JSON instead of CSV, but it works pretty much the same.  
Just make sure to first load the assets from the content browser, then you’ll be able to write it.

Here is the code I’m using to update a DataTable from a JSON file on disk using a Struct already available in the content browser

```auto
# Load DataTable and Struct assets
data_table = unreal.EditorAssetLibrary.load_asset(ue_asset_path)
data_structure = unreal.EditorAssetLibrary.load_asset(datastructure_source_path + datastructures)

# Fill in data from json file, and save DataTable asset 
unreal.DataTableFunctionLibrary.fill_data_table_from_json_file(data_table, json_file_path, data_structure)
unreal.EditorAssetLibrary.save_loaded_asset(data_table, True)

```

I’m not sure if having the Struct made in C++ makes any difference in term of how you can access it through python.

Hope this helps

---

<div class="post-metadata">

**Author:** ![Hansyuan\_Nb](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hansyuan_nb/32/561253_2.png) [@Hansyuan\_Nb](https://forums.unrealengine.com/u/Hansyuan_Nb)\
**Post date:** [June 8, 2022, 4:11am UTC](https://forums.unrealengine.com/t/reimport-datatable-csv-with-python/567768/4 "2022-06-08T04:11:47Z")

</div>

Finally I got it right.

```auto
fpath = r"E:\dev\XXXEngine\UnrealEngine\GameXXX\Saved\CSV\MyDataTable.csv" #Use csv to find the coresponse data table.
dpath = r'/Game/_XXX/DataTable/BattleMap' #only provide the folder
 
def my_reimport():
    task = unreal.AssetImportTask()
    task.filename = fpath
    task.destination_path = dpath
    task.replace_existing = True
    task.automated = True
    task.save = False
 
    csv_factory = unreal.CSVImportFactory()
    csv_factory.automated_import_settings.import_row_struct = unreal.load_object(None, '/Game/_XXX/DataTable/BattleMap/MyTestStruct.MyTestStruct') #use unreal.load_object to load struct type as an object.
 
 
    task.factory = csv_factory
 
 
    asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
    asset_tools.import_asset_tasks([task])
 
my_reimport()

```
