Skip to main content
 首页 » 编程设计

python之我如何调整我的代码以仅删除打印所需的值

2024年09月07日8telwanggs

我当前的代码:

def write_from_dict(users_folder): 
    for key, value in value_dict.items():  # iterate over the dict 
        file_path = os.path.join(users_folder, key + '.txt') 
        with open(file_path, 'w') as f:       # open the file for writing 
            for line in value:                # iterate over the lists 
 
                f.write('{}\n'.format(line)) 

我当前的输出:

['4802', '156', '4770', '141'] 
['4895', '157', '4810', '141'] 
['4923', '156', '4903', '145'] 

我想要的输出:

4802,156,4770,141 
4895,157,4810,141 
4923,156,4903,145 

所以基本上我想删除空格 '' 和 []。

请您参考如下方法:

替换

f.write('{}\n'.format(line)) 

与

f.write('{}\n'.format(",".join(line)))