0

ファイルに参加して次のエラーが発生した後、リストのいくつかの値を書き込もうとしていますが、これを修正する方法についての入力はありますか?

data_changes ={'305403': ['302180'], '312994': ['311957'], '311957': ['312621'] }
modem_changes = {'305403': [], '313113': [], '312994': ['253036', '312591'], '311957': []}

dc_set = set(data_changes)
mc_set = set(modem_changes)

with open('file.txt', 'w+') as f:
    for key in dc_set.intersection(mc_set):
        union_values = data_changes[key] + modem_changes[key]
        values = key, ','.join(union_values)
        f.write(values)

with open('file.txt', 'a') as f:
    for key in dc_set.symmetric_difference(mc_set):
        dc_values = data_changes.get(key) or []
        mc_values = data_changes.get(key) or []
        union_values = dc_values + mc_values
        values=key, ','.join(union_values)
        f.write(values)

エラー:-

Traceback (most recent call last):
  File "final_build_list.py", line 31, in <module>
    f.write(values)
TypeError: argument 1 must be string or read-only character buffer, not tuple
4

1 に答える 1

5

values=key, ','.join(union_values)tuplenotstringであり、タプルをファイルに書き込むことはできません。

>>> foo = 1,2 # comma separated values on RHS are converted to tuple
>>> type(foo)
<type 'tuple'>

あなたが探していたかもしれません:

values= "{0}, {1}\n".format(key, ','.join(union_values))

例:

data_changes ={'305403': ['302180'], '312994': ['311957'], '311957': ['312621'] }
modem_changes = {'305403': [], '313113': [], '312994': ['253036', '312591'], '311957': []}

dc_set = set(data_changes)
mc_set = set(modem_changes)

with open('abc', 'w+') as f:
    for key in dc_set.intersection(mc_set):
        union_values = data_changes[key] + modem_changes[key]
        values= "{0}, {1}\n".format(key, ','.join(union_values))
        f.write(values)

出力 (abc のコンテンツ):

311957, 312621
305403, 302180
312994, 311957,253036,312591
于 2013-05-02T07:28:22.247 に答える