remove tab character from csv data or from mtext

remove tab character from csv data or from mtext

Gorra
Advocate Advocate
1,957件の閲覧回数
17件の返信
メッセージ1/18

remove tab character from csv data or from mtext

Gorra
Advocate
Advocate

Hello,

I have a csv that I extract data from and update block attributes and properties, then summarized the updated information in an mtext. All of this is working fine, except that some of the csv fields have a tab character at the start of the text, which shows up in the final mtext. The field extracts as a text, as a variable ADDRESSv. My question is would I  remove the tab using its ASCII code, or is there some other identifier I should use?

 

Thanks,

Gorra

 

0 件のいいね
解決済み
1,958件の閲覧回数
17件の返信
返信 (17)
メッセージ2/18

komondormrex
Mentor
Mentor

hi,

tab in a string may be  as the "\t" substring. 

see there prin1.

0 件のいいね
メッセージ3/18

paullimapa
Mentor
Mentor

best if you upload a sample csv file with at least one row that has that tab character in front of the text that you want to remove so community then can run tests


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 件のいいね
メッセージ4/18

ec-cad
Collaborator
Collaborator

Could you upload a .csv with the 'tabs' in there, so we can check what to strip ?

ECCAD

0 件のいいね
メッセージ5/18

ec-cad
Collaborator
Collaborator

You can 'foreach text csvfile, call this routine to strip those tabs.

ECCAD

;; Strip Tab Characters

 (defun strip_tabs ( str )
  (setq L (strlen str)); length of the input string
  (setq ret "" N 1)
  (repeat L
   (setq ch1 (substr str N 1)); single character
   (if (/= ch1 "\t")
    (setq ret (strcat ret ch1))
   ); if
   (if (= ch1 "\t")
    (setq ret (strcat ret " ")); replace with a 'Space
   ); if
   (setq N (+ N 1))
  ); repeat
   ret
); defun
;; Testing

(setq S "TEST\tWITH\tTABS\tIN THE STRING.")

(setq TXT (strip_tabs S))
(princ TXT)

 

0 件のいいね
メッセージ6/18

Gorra
Advocate
Advocate

Thanks @komondormrex, that's part of what I'm looking for

0 件のいいね
メッセージ7/18

Gorra
Advocate
Advocate

@ec-cad @paullimapa @komondormrex 

 

One of the offending lines is attached.

 

I tried this function:

(defun RemoveTab (Ln / Chrs)
  (setq Chrs (ascii Ln))
  (if (eq Chrs 9)
    (vl-string-left-trim "\t" Ln)
    (princ)
  )
)

 

It ran without errors, but didn't take the tabs out. I'll be trying ec-cad's method this afternoon, unless there's something simple stopping the above from working.

 

The target data is column N, so position 13 on the list for the row.

Gorra

 

 

 

0 件のいいね
メッセージ8/18

komondormrex
Mentor
Mentor

and the other part(s)? 

0 件のいいね
メッセージ9/18

Gorra
Advocate
Advocate

Sorry, now edited to include more

0 件のいいね
メッセージ10/18

komondormrex
Mentor
Mentor
解決済み

the function will remove all tabs from a string and return tat string.

(defun RemoveTab (line)
     (vl-list->string (vl-remove 9 (vl-string->list line)))
)

but if you want to rewrite your csv you need firstly to open your csv, read all lines from it into a list, close it, process lines read to the list, open again your csv for writing and write to it all lines processed with the function removetab from the list.

0 件のいいね
メッセージ11/18

Kent1Cooper
Consultant
Consultant

Can you do this with the line read from the .csv file, here put into a variable called 'txt', before splitting it up around the commas?  The (while) part is because the string substitution does only one at a time, in case some line(s) may have more than one.

 

(while (wcmatch txt "*\t*")

  (setq txt (vl-string-subst "" "\t" txt))

)

Kent Cooper, AIA
0 件のいいね
メッセージ12/18

Gorra
Advocate
Advocate

@komondormrex 

Does the code automatically take the 9 as an ascii character? It runs without error, but the tab is still showing up. I tried replacing the 9 with \t, "\t", and 't but no change

 

Gorra

0 件のいいね
メッセージ13/18

Gorra
Advocate
Advocate

@Kent1Cooper 

Do you mean in the LM:csv->lst function?

 

Gorra

0 件のいいね
メッセージ14/18

Kent1Cooper
Consultant
Consultant

@Gorra wrote:

.... Do you mean in the LM:csv->lst function? ....


If that's what you use to split the line from the .csv file into separate text strings around the commas.  You could also split it up first, and then do the substitution of an empty string for the tab on each part.  But if you do it to the whole string first, you don't need to do anything about that with the individual strings.

Kent Cooper, AIA
0 件のいいね
メッセージ15/18

komondormrex
Mentor
Mentor

can't be.

see (RemoveTab "\t\t\twqwqqw,\"\tdsdssds\",\t212121,\t21121")-> "wqwqqw,\"dsdssds\",212121,21121".

all tabs are gone.

and yes the code takes all tabs (ascii code 9) from the string, converted to list of ascii codes and returns that string.

and the whole thing may be done as follows

 

;***********************************************************

(defun RemoveTab (line)
     (vl-list->string (vl-remove 9 (vl-string->list line)))
)

;***********************************************************

(defun c:de_tab_csv (/ csv_list csv_file csv_file_id line)
	(setq csv_file "your_drive:\\your_path\\your_file.csv"
		  csv_file_id (open csv_file "r")
	)
	(while (setq line (read-line csv_file_id))
		(setq csv_list (append csv_list (list line)))
	)
	(close csv_file_id)
	(setq csv_file_id (open csv_file "w"))
	(foreach line csv_list 
		(write-line (RemoveTab line) csv_file_id)
	)
	(close csv_file_id)
)

;***********************************************************

updated

 

0 件のいいね
メッセージ16/18

Gorra
Advocate
Advocate

@komondormrex 

It shouldn't be, but it is (attached). Could it be something other than a tab? I've been assuming it is because when I click on the cell, the gap in the formula bar is all one character.

 

Gorra

0 件のいいね
メッセージ17/18

Gorra
Advocate
Advocate

Ah, I just tested it by princ-ing the variable, and it is coming out as 9 for all the lines affected.

0 件のいいね
メッセージ18/18

Gorra
Advocate
Advocate

Ugh, I wasn't redefining the main variable with the updated string.

 

Thanks everyone, I'll now hide my head somewhere.

 

Gorra

0 件のいいね