In attempting to read a webpage ("https://www.house.gov/representatives#by-name") I have managed to import a table using

tableText1 = WebExecute[browser, "JavascriptExecute" -> "return document.getElementById('by-name').innerText;"];

I've eventually been able to massage the text, which contains a number of line feeds and blanks and comas at different levels within the table as well as a variable number of columns in each row, into a single table of Dimensions[n,6] of which [11,3] are shown here. However, note that the now third column contains a rather inconsistent series of strings, which I would like to edit following each committee name by a linefeed ("\n") using a list of rules.

The subset of data for illustrative purposes is here:

table21 = {"Arrington, Jodey", "Texas 19th", "Ways and Means"}, 
{"Axne, Cynthia", "Iowa 3rd","AgricultureFinancial Services"}, 
{"Babin, Brian", "Texas 36th", "Transportation and InfrastructureScience, 
Space, and Technology"}, \
{"Bacon, Don", "Nebraska 2nd", "AgricultureArmed Services"}, 
{"Baird, James", "Indiana 4th","AgricultureScience, Space, and Technology"}, 
{"Balderson, Troy", "Ohio 12th","Transportation and InfrastructureSmall 
BusinessScience, Space, and Technology"}, 
{"Banks, Jim", "Indiana 3rd", "Armed ServicesEducation and LaborVeterans' 
{"Barr, Andy", "Kentucky 6th","Financial ServicesVeterans' Affairs"}, 
{"Barragán, Nanette", "California 44th","Homeland SecurityEnergy and 
{"Bass, Karen","California 37th", "Foreign AffairsJudiciary"}, 
{"Beatty, Joyce", "Ohio 3rd", "Financial Services"}}

As one can see the committee names in the 3rd column are not in a columnar form for each representative and the names run together. Because in the larger list there are many combinations of committees that can not be easily replaced by a single StringReplace[table21,{"a"->"a\n, "b->b\n"...}] expression, I have created a list of rules using Thread as follows:

committees = {"Agriculture", "Appropriations", "Armed Services", 
"Budget", "Climate Change", "Education and Labor", "Ethics", 
"Energy and Commerce", "Financial Services", "Foreign Affairs", 
"Homeland Security", "Intelligence", "Judiciary", 
"Modernization of Congress", "Natural Resources", 
"Oversight and Reform", "Science, Space and Technology", 
"Small Business", "Transportation and Infrastrure", 
"Veterans' Affairs", "Ways and Means"}
addlinefeed[x_] := x ~~ "\n";
editedcommittees = addlinefeed /@ committees;
rules = Thread[committees -> editedcommittees];

Although I can convert the entire table to a String by doing a StringReplace to effect these edits, in doing so I lose the desirable list structure and getting it back is complicated by the presence of comas used differently in different elements within the list of lists.

How can I replace one column of this list of lists suitably edited using StringReplace in combination with my set of rules so that only this single suitably edited column appears as a column of columns within the list of lists, while leaving other columns within the list of lists unchanged?

I've tried various combinations of Replace, StringReplace, Map, and MapAt trying to take into account the suitable levels and parts to edit this one column leaving all others the same. However, I have not met with success. A few other questions give hints but I am stumped. How can this be done?


1 Answer 1

Grid[MapAt[StringReplace[#, (l_ ~~ u_) /; 
   UpperCaseQ[u] && LowerCaseQ[l] :> l <> "\n" <> u] &, 
   table21, {All, 3}], Frame -> All, Alignment -> Left, 
   Background -> {None, {{LightOrange, White}}}]

enter image description here


Based on comments, I think you can start defining a set such as

committees = {"Agriculture", "Appropriations", "Armed Services", 
"Budget", "Climate Change", "Education and Labor", "Ethics", 
"Energy and Commerce", "Financial Services", "Foreign Affairs", 
"Homeland Security", "Intelligence", "Judiciary", 
"Modernization of Congress", "Natural Resources", 
"Oversight and Reform", "Science, Space, and Technology", 
"Small Business", "Transportation and Infrastructure", 
"Veterans' Affairs", "Ways and Means", "Small Business", 
"Science, \nSpace, and Technology" -> 
"Science, Space, and Technology", 
"Small \nBusiness" -> "Small Business", 
"Veterans' \nAffairs" -> "Veterans' Affairs", 
"Energy and \nCommerce" -> "Energy and Commerce", 
"Intelligence" -> "Permanent Select Committee on Intelligence"};

and then use that as follows

Grid[MapAt[StringRiffle[StringCases[#, committees], "\n"] &, table21, {All, 3}], 
 Frame -> All, Alignment -> Left, Background -> {None, {{LightOrange, White}}}]

enter image description here

  • $\begingroup$ Both of these answers get the job done in this special case. However, this will only work in cases such as I have described here, where the change being made is a simple edit (ie appending a "\n" accross all rows). I was thinking more in terms of an answer that could handle a more complicated sets of rules that might generate different edits for different rows that is for a more general case of rules, depending on the contents of the [[row,column]]. $\endgroup$ Oct 18, 2019 at 14:45
  • $\begingroup$ For edits depending on the contents of the [[row,column]], for example say substituting the official committee names for some of the committees (ie "Permanent Select Committee on Intelligence" when given only "Intelligence" "Intelligence"-> "Permanent ... Intelligence"). In second example "Energy and Commerce" are split across multiple lines, which presumably derives from alignment. In second under "Banks" "Armed ServicesEducation and Labor" remain unedited. $\endgroup$ Oct 18, 2019 at 14:54
  • $\begingroup$ Perhaps, my effort to generate a simple, quick and dirty set of rules obscured the question I am trying to get at. $\endgroup$ Oct 18, 2019 at 15:00
  • $\begingroup$ Yes. Could you clarify the question itself. That way for posterity the answers will reflect the question, and not the question and obscured comments. $\endgroup$ Oct 18, 2019 at 15:02
  • $\begingroup$ Thank you. The follow up in your answer relating to the structure of the committee's list is exactly what I was looking for. I didn't think to include rules within the list, but now I see that is exactly what I was asking for. $\endgroup$ Oct 19, 2019 at 3:54

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.