Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

pandas dataframe to string for posting in jira comment

Zak S November 28, 2023

I am trying to create a string representation of a pandas DataFrame that can be formatted as a table in JIRA comments. My issue arises when newline characters (\n) in the string are not being correctly interpreted by JIRA, when I try to paste the string.

Code: Here's the Python function I'm using to convert a DataFrame to a JIRA-formatted table string:

import pandas as pd

def dataframe_to_jira_string(df):
    jira_lines = []
    header = "||" + "||".join([f"*{col}*" for col in df.columns]) + "||"
    jira_lines.append(header)

    for index, row in df.iterrows():
        first_col = f"|{index}"
        other_cols = "|".join(row.astype(str).values)
        row_str = f"{first_col}|{other_cols}|"
        jira_lines.append(row_str)

    jira_table_string = "\n".join(jira_lines)
    return jira_table_string

# Sample DataFrame
data = {
    'CR': [4312, 1432, 7, 41, 1, 9, 11],
    'False': [13, 1, 7, 0, 30, 2, 4],
    'Miss': [5285, 250, 15, 45, 0, 57, 2],
    'True': [732, 48, 105, 19, 63, 10, 9]
}
index = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
df = pd.DataFrame(data, index=index)

jira_table_string = dataframe_to_jira_string(df)

Question: How can I ensure that the newline characters in the string are preserved and correctly interpreted by JIRA when pasted? Is there a specific way I should format the string or handle newline characters in Python to make it compatible with JIRA's text processing? Any advice or insights on how to solve this issue would be greatly appreciated!

0 answers

Suggest an answer

Log in or Sign up to answer