-2

メンバー、

私は何時間も頭を悩ませています。コードが機能しないのはなぜですか。

CSV ファイルからデータを取得しています。2 つのコードが一致するときに、1 つの値を変更しようとしています。

def mode(x):
for line in data_df['Bar_Code']:
    #print(line) ~ Line outputs the Barcodes in the Bar_Code Column of the CSV file.
    with open("Barcodes_to_match.txt") as barcodes:
        for barcode in barcodes:
            #print(barcode) ~ Outputs the Barcode that it need to be matched.
            if barcode == line:
                x = x.replace("True", "False")
                return x

data_df['Published'] = data_df['Published'].apply(lambda x: mode(x))

data_df.to_csv(('Products.csv'), encoding='UTF-8', quoting=csv.QUOTE_ALL, quotechar='"', index=None, sep=str(","))

Product.csv ファイル:

Bar_Code, Product, Published
BLA00BLA00, Product1, True
BLU00BLU00, Product2, False
BLI00BLI00, Product3, True
BLE00BLE00, Product4, False

Barcodes_to_match.txt:

BLA00BLA00
BLI00BLI00

したがって、製品行 (BLA00BLA00 および BLI00BLI00)の値を変更する必要があります。公開された列をFalseにします。

誰かが私の方法をチェックして値を置き換えることができますか?

ありがとう!

編集:印刷コマンドを追加すると、無限ループの最後の試合でコードがストックされているようです。

if barcode == line:
   print(barcode + " = " + line)
   x = x.replace("True", "False")
   return x

Output:
BLI00BLI00 = BLI00BLI00
BLI00BLI00 = BLI00BLI00
BLI00BLI00 = BLI00BLI00
BLI00BLI00 = BLI00BLI00
BLI00BLI00 = BLI00BLI00
BLI00BLI00 = BLI00BLI00
BLI00BLI00 = BLI00BLI00
BLI00BLI00 = BLI00BLI00
4

1 に答える 1