Robotic Process Automation (RPA)

 View Only
  • 1.  Write table to file using tab as delimiter

    Posted Fri April 14, 2023 12:28 PM

    Hello, everybody.

    I need to use the command write a table to a txt file, this file needs to be separated by tabs. ¿How can I do that? I can't directly enter the tab in the parameter and I tried with scape characters like \t.

    Thank you.



    ------------------------------
    Jose Luis Rodriguez Gonzalez
    ------------------------------


  • 2.  RE: Write table to file using tab as delimiter

    Posted Thu May 25, 2023 12:57 PM
    Hi José,
     
    Here is an example of how to transform a table into a TAB-delimited file.
     
    defVar --name tab --type String
    defVar --name fullText --type String
    defVar --name defaultPath --type String
    defVar --name dataTable --type DataTable
    setVar --name "${tab}" --value "\t"
    jsonToTable --handleError  --json "  {\n    \"person\": [\n      {\n        \"firstName\": \"John\",\n        \"lastName\": \"doe\",\n        \"age\": 26\n      },\n      {\n        \"firstName\": \"Smith\",\n        \"lastName\": \"zen\",\n        \"age\": 45\n      }\n    ]\n  }" --jsonPath "$.person" --comment "I used a json to create a sample table" dataTable=value
    setVar --name "${fullText}" --value "${dataTable}"
    trimString --text "${fullText}" --trimoption "TrimStartAndEnd" fullText=value
    replaceText --texttoparse "${fullText}" --textpattern "," --replacement "${tab}" fullText=value
    getSpecialFolder --folder "Desktop" defaultPath=value
    writeToFile --value "firstName${tab}lastName${tab}age\r\n${fullText}" --file "${defaultPath}\\person.txt" --encoding "Default" --overwrite

    I hope it helps you.
     
    Cheers.


    ------------------------------
    Angelo Alves
    IBM RPA Technical Specialist
    ------------------------------



  • 3.  RE: Write table to file using tab as delimiter

    Posted Fri May 24, 2024 06:02 AM

    I had to use "Save Office File As" plain text. In this case Office saves the Excel Table with a TAB delimiter.



    ------------------------------
    Milan Babčanec
    ------------------------------



  • 4.  RE: Write table to file using tab as delimiter

    Posted Mon May 27, 2024 02:25 AM

    Hi Jose Luis,

    To write a table to a text file with tabs as delimiters, you can use a Python script. Here's a simple example to demonstrate how you can achieve this:

    # Define your table as a list of lists
    table = [
        ["Name", "Age", "City"],
        ["Alice", "30", "New York"],
        ["Bob", "25", "Los Angeles"],
        ["Charlie", "35", "Chicago"]
    ]

    # Open a file in write mode
    with open('table.txt', 'w') as file:


        # Iterate over each row in the table
        for row in table:


            # Join each cell in the row with a tab character and write to the file
            file.write('\t'.join(row) + '\n')

    print("Table written to 'table.txt' with tabs as delimiters.")

    This script does the following:
    1. Defines a sample table as a list of lists.
    2. Opens a file named table.txt in write mode.
    3. Iterates over each row in the table, joins the cells in the row with a tab character (\t), and writes the resulting string to the file followed by a newline character (\n).
    You can adjust the table variable to match your actual data. After running this script, you should find your data neatly organized in table.txt, with each column separated by a tab.
    If you need any further assistance, feel free to ask!


    ------------------------------
    Jude Ighomena
    Senior Manager, Core Network Operations
    Broadbased Communications Limited
    Lagos, Nigeria
    +2348163474613
    ------------------------------



  • 5.  RE: Write table to file using tab as delimiter

    IBM Champion
    Posted Mon May 27, 2024 06:26 AM

    Here's how you can write a table to a txt file with tab separators, even though you can't directly enter a tab character in the parameter:

    Method :String Concatenation:

    1. Loop through your table data: Use a loop to iterate through each row of your table data.
    2. Build the line string: Inside the loop, for each row, construct a string variable. Concatenate each data element with a tab character (\t) in between. You can achieve this using string manipulation functions provided by IBM RPA.

    Here's an example (assuming your data is stored in a variable named tableData):

    <response-element _nghost-ng-c1192139400="" ng-version="0.0.0-PLACEHOLDER"></response-element>

    Python
    # Empty string to hold the line
    line_string = ""
    
    # Loop through each row
    for row in tableData:
      # Concatenate each element with a tab
      for element in row:
        line_string += element + "\t"
      # Remove the last tab from the line (optional)
      line_string = line_string[:-1]
      # Add a newline character for each row
      line_string += "\n"
    
    # Write the string to the file
    write_to_file("output.txt", line_string)



    ------------------------------
    Rakesh Ghoshal
    Principal Solution Architect

    Gulf Business Machines
    E-Mail: rghshal@gbmme.com
    Linkedin: www.linkedin.com/in/rkg-kw
    PO Box 4175, Safat, Kuwait
    General Marketing & Services Representative for IBM WTC
    www.gbmme.com
    ------------------------------