Python code to import ASCII data - not working

Can anyone explain why this python code does not work?
It completes without errors but no data import takes place.

import os
import win32com.client

ab = win32com.client.Dispatch("Broker.Application")
dbase = 'C:/Program Files/AmiBroker/Databases/MyDatabase'

ab.Import(0, 'C:/Data/Update.txt', 'C:/Program Files/AmiBroker/Formats/txt.format')

ab = None

(The same import succeeds when run manually from the AB menu or from a AB batch.)

Or, does anyone have a vbs or js code that works for ASCII import?

Save the code below as Sample.BAT and execute it by double clicking it.

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment

:: ******* Begin batch code *********
@echo off
c:\Windows\SysWOW64\cscript.exe //E:JScript //nologo "%~f0"
exit /b

********* Begin JScript code **********/

var AmiDatabase = "C:\\Program Files\\AmiBroker\\Databases\\MyDatabase";
var fileToImport = "C:\\Data\\Update.txt";

WScript.StdOut.Write( "Import data (Y/N) ?");
var a = WScript.StdIn.ReadLine();
if( a.charAt(0).toUpperCase() == "Y" ) {
	AB = new ActiveXObject( "Broker.Application" ); 

	try	{
		// amibroker visible;
		AB.Visible = 1; 
		AB.LoadDatabase( AmiDatabase );
		WScript.StdOut.WriteLine( "Version: " + AB.Version + " DBPath: " + AB.DatabasePath );
		AB.Import(0, fileToImport, "txt.format"); 
		WScript.StdOut.Write( "<enter> to close AB");
	catch ( err ) {
		WScript.echo( "Exception: " + err.message );
WScript.StdOut.Write( "<enter> to exit");
1 Like

@wciszak, in your Python script, try modifying the following line (remove the "format" path):

ab.Import(0, 'C:/Data/Update.txt', 'txt.format')

I assume that you must have already created the correct import format (using the "Import Wizard" where you tried with success to import data from the "update.txt" file or another similarly formatted data file).

I dont think there is any problem with code.

forward-slash python style path separators won't work.
You need to change / to \ for import_file and import_format

'C:\\Data\\Update.txt', 'C:\\Program Files\\AmiBroker\\Formats\\txt.format'

You can also test python r-string and single \ if you dont want to escape like this \

Thanks nsm51
Your simple solution worked like a charm!