3
$\begingroup$

I think this a fairly basic question but I'm a relative newcomer to coding and would appreciate any help.

I have a list of lists. Within the lists there are a mixture of strings and numbers. I want to apply a rule to only the strings within the lists. My goal is to clean up the text and remove stuff like ''.

This is my list of lists:

data={{900, "WS", "PWS", "O", 3, "Watermaker 2 leaking", 
  "<div>Watermaker 2 was leaking at one of the fittings near the \
PRV.</div>", 1, "AHill", "02:00.0", "NULL", "NULL", "00:00.0", 
  "Ktaylor", "NULL", "<div>Straps replaced and leak fixed</div>", 
  "00:00.0", "00:00.0", 2, 0, 
  "<div>OEM design an specification.</div>", 182, "NULL", "NULL", 
  "NULL", "NULL", "NULL", "NULL", "NULL", 
  "Equipment in hazardous condition", ""}, {900, "WS", "PWS", "O", 2, 
  "No 1 and 2 RO Plant Feed Backwash Earth Straps Detached", 
  "<div>During visual inspection of the RO plants it was found that \
the earth connections on the feed backwash pumps on No1 &amp; No2 \
were detached. </div>", 1, "rmilne", "06:00.0", "NULL", "NULL", 
  "00:00.0", "rmilne", "NULL", "<div>Straps replaced.</div>", 
  "00:00.0", "00:00.0", 2, 0, 
  "<div>IEC 61140 : 1997 Protection against electric shock \[Dash] \
common aspects for installation and equipment. 5.1 Basic protection \
shall consist of one or more provisions that under normal conditions \
prevent contact with hazardous-live-parts</div>", 15, "NULL", "NULL", 
  "NULL", "NULL", "NULL", "NULL", "NULL", "Electrical safety", ""}};

This is the code I tried to use:

MapThread[
 If[StringQ[#], 
   StringReplace[#, {"<", ">", "div", "/"} -> ""], #] &, data]

This returns this:

{900, "WS", "PWS", "O", 3, "Watermaker 2 leaking", "Watermaker 2 was \
leaking at one of the fittings near the PRV.", 1, "AHill", "02:00.0", \
"NULL", "NULL", "00:00.0", "Ktaylor", "NULL", "Straps replaced and \
leak fixed", "00:00.0", "00:00.0", 2, 0, "OEM design an \
specification.", 182, "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", \
"NULL", "Equipment in hazardous condition", ""}

This works well but only for the first list, the second list seems to be ignored. What do I need to add to apply to all lists?

Thank you in advance!

$\endgroup$

1 Answer 1

2
$\begingroup$
Map[ReplaceAll[ s_String :> StringTrim[s, "<div>" | "</div>"] ]] @ data

enter image description here

Alternatively, your code modified slightly:

Map[If[StringQ[#], 
   StringReplace[#, {"<", ">", "div", "/"} -> ""], #] &, data, 2]

same result

$\endgroup$
1
  • $\begingroup$ That's great, much appreciated! $\endgroup$
    – wmv
    Jan 11, 2022 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.