将元组转换为String并将方括号替换为圆括号



我试图用我已经转换成字符串的圆括号替换元组中的方括号,然后试图替换。下面是我正在尝试的代码。

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)

但是这里打印的是['ABCXYZ-I', 'ABCXYZ-I', 'ABCXYZ-I']。我想打印('ABCXYZ-I', 'ABCXYZ-I', 'ABCXYZ-I')。我遗漏了什么吗?

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

.replace返回新字符串,但是您只是将返回值丢掉。你需要做一些像

这样的事情
table_tpl = table_tpl.replace('[','(')
table_tpl = table_tpl.replace(']',')')

最新更新