Skip to content
Advertisement

Convert a tuple into a String and replace square brackets with round ones Python

I am trying to replace square brackets in a tuple with round brackets which I had converted into a String and then tried to replace. Below is the code I am trying.

def fetch_values_from_csv_and_store_it_in_tuple(test_case_name,file_name):
    df = expected_df = read_csv(
        "{}/output/Float_Ingestion_Expected_Output_files/{}/{}.csv".format(str(parentDir), test_case_name, file_name),
        ',', False)
    # list of strings
    tables = list(df["factset_entity_id"])
    # list of single tuples
    table_tuples = [(t) for t in df["factset_entity_id"]]
    table_tpl=str(table_tuples)
    table_tpl.replace('[','(')
    table_tpl.replace(']',')')
    print(table_tpl)

But this is printing [‘ABCXYZ-I’, ‘ABCXYZ-I’, ‘ABCXYZ-I’]. I want to print (‘ABCXYZ-I’, ‘ABCXYZ-I’, ‘ABCXYZ-I’). Am I missing something here?

Advertisement

Answer

table_tpl.replace('[','(')

.replace returns the new string, but you are just throwing the return value away. You will have to do something like

table_tpl = table_tpl.replace('[','(')
table_tpl = table_tpl.replace(']',')')
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement