Powerbuilder-删除最后一个分隔符
Powerbuilder-Remove last separator
您好,我正在使用 PB 12,但我遇到了 SaveasFormattedText
的问题
函数运行良好,但分隔符字符在每行末尾添加一个字符,我需要删除最后一个分隔符。
例如,saveasformattedtext 导出数据是这样的:
data1;data2;data3;data4;
data1;data2;data3;data4;
etc.
我需要这样:
data1;data2;data3;data4
data1;data2;data3;data4
没有“;”在最后。
这是我的代码:
dw_report.SaveAsFormattedText("d:/RESULTS2.TXT", EncodingUTF8! , "|", "","~r~n",True)
请帮忙
您可以编写一个函数来替换 "; 连接的组合
char(10) char(13) " 只能是 char(10)char(13)。(我可能有
CR/LF 这里反转了ascii码)。
试试下面的功能。您可能希望根据需要添加一些错误检查和更多选项。这个想法是将每一行单独保存为文件中的一行。
/*function: f_dw_saveasformattedtext(dw_1, ls_filename, ';')
parameters: adw_dw(datawindow/datastore), as_filename (string), as_separator(string)
*/
long ll_row, ll_rows, ll_colcount, ll_colindex
string ls_colname, ls_coltype, ls_value, ls_lineval
int li_filenum
any la_anyval
ll_rows = adw_dw.rowcount()
ll_colcount = long(adw_dw.Describe("DataWindow.Column.Count"))
li_filenum = FileOpen(as_filename, LineMode!, Write!, LockWrite!, Append!)
if li_filenum = -1 or isnull(li_filenum) = true then
return -1
end if
for ll_row = 1 to ll_rows
ls_lineval = ''
for ll_colindex = 1 to ll_colcount
ls_colname = adw_dw.describe("#" + string(ll_colindex) + ".Name")
ls_coltype = adw_dw.Describe ( ls_colname + ".ColType" )
CHOOSE CASE Lower ( Left ( ls_coltype , 5 ) )
CASE "char(", "char","strin" // CHARACTER DATATYPE
la_anyval = adw_dw.GetItemString ( ll_row, ls_colname )
CASE "date" // DATE DATATYPE
la_anyval = adw_dw.GetItemDate ( ll_row, ls_colname )
CASE "datet" // DATETIME DATATYPE
la_anyval = adw_dw.GetItemDateTime ( ll_row, ls_colname )
CASE "decim" // DECIMAL DATATYPE
la_anyval = adw_dw.GetItemDecimal ( ll_row, ls_colname )
CASE "numbe", "long", "ulong", "real", "int" // NUMBER DATATYPE
la_anyval = adw_dw.GetItemNumber ( ll_row, ls_colname )
CASE "time", "times" // TIME DATATYPE
la_anyval = adw_dw.GetItemTime ( ll_row, ls_colname )
CASE ELSE
SetNull ( la_anyval )
END CHOOSE
ls_value = string(la_anyval)
if trim(ls_lineval) = '' then
ls_lineval = ls_value
else
ls_lineval += as_separator + ls_value
end if
next //columns
FileWrite(li_filenum, ls_lineval)
next //rows
return 1
您好,我正在使用 PB 12,但我遇到了 SaveasFormattedText
函数运行良好,但分隔符字符在每行末尾添加一个字符,我需要删除最后一个分隔符。
例如,saveasformattedtext 导出数据是这样的:
data1;data2;data3;data4;
data1;data2;data3;data4;
etc.
我需要这样:
data1;data2;data3;data4
data1;data2;data3;data4
没有“;”在最后。
这是我的代码:
dw_report.SaveAsFormattedText("d:/RESULTS2.TXT", EncodingUTF8! , "|", "","~r~n",True)
请帮忙
您可以编写一个函数来替换 "; 连接的组合 char(10) char(13) " 只能是 char(10)char(13)。(我可能有 CR/LF 这里反转了ascii码)。
试试下面的功能。您可能希望根据需要添加一些错误检查和更多选项。这个想法是将每一行单独保存为文件中的一行。
/*function: f_dw_saveasformattedtext(dw_1, ls_filename, ';')
parameters: adw_dw(datawindow/datastore), as_filename (string), as_separator(string)
*/
long ll_row, ll_rows, ll_colcount, ll_colindex
string ls_colname, ls_coltype, ls_value, ls_lineval
int li_filenum
any la_anyval
ll_rows = adw_dw.rowcount()
ll_colcount = long(adw_dw.Describe("DataWindow.Column.Count"))
li_filenum = FileOpen(as_filename, LineMode!, Write!, LockWrite!, Append!)
if li_filenum = -1 or isnull(li_filenum) = true then
return -1
end if
for ll_row = 1 to ll_rows
ls_lineval = ''
for ll_colindex = 1 to ll_colcount
ls_colname = adw_dw.describe("#" + string(ll_colindex) + ".Name")
ls_coltype = adw_dw.Describe ( ls_colname + ".ColType" )
CHOOSE CASE Lower ( Left ( ls_coltype , 5 ) )
CASE "char(", "char","strin" // CHARACTER DATATYPE
la_anyval = adw_dw.GetItemString ( ll_row, ls_colname )
CASE "date" // DATE DATATYPE
la_anyval = adw_dw.GetItemDate ( ll_row, ls_colname )
CASE "datet" // DATETIME DATATYPE
la_anyval = adw_dw.GetItemDateTime ( ll_row, ls_colname )
CASE "decim" // DECIMAL DATATYPE
la_anyval = adw_dw.GetItemDecimal ( ll_row, ls_colname )
CASE "numbe", "long", "ulong", "real", "int" // NUMBER DATATYPE
la_anyval = adw_dw.GetItemNumber ( ll_row, ls_colname )
CASE "time", "times" // TIME DATATYPE
la_anyval = adw_dw.GetItemTime ( ll_row, ls_colname )
CASE ELSE
SetNull ( la_anyval )
END CHOOSE
ls_value = string(la_anyval)
if trim(ls_lineval) = '' then
ls_lineval = ls_value
else
ls_lineval += as_separator + ls_value
end if
next //columns
FileWrite(li_filenum, ls_lineval)
next //rows
return 1