检测到 Python 错误不一致的意图



我正在摆弄一个python脚本,用于替换制表符分隔文本文件的特定列中的某些字符。

如果我运行脚本,则会出现错误:检测到不一致的意图。

import csv
# File names: to read in from and read out to
input_file = "test.txt"
output_file = input_file + "-SA_input.txt"
with open(input_file) as to_read:
with open(output_file, "wb") as tmp_file:
    reader = csv.reader(to_read, delimiter = "t")
    writer = csv.writer(tmp_file)
    desired_column = [1]        # text column
    for row in reader:     # read one row at a time
        myColumn = row[desired_column]   # build the output row (process)
        myColumn.replace('0', ' ')
        myColumn.replace('1', ' ')
        myColumn.replace('2', ' ')
        myColumn.replace('3', ' ')
        myColumn.replace('4', ' ')
        myColumn.replace('5', ' ')
        myColumn.replace('6', ' ')
        myColumn.replace('7', ' ')
        myColumn.replace('8', ' ')
        myColumn.replace('9', ' ')
        writer.writerow(row) # write it

非常感谢帮助! :)

而不是

with open(input_file) as to_read:
with open(output_file, "wb") as tmp_file:

你需要使用

with open(input_file) as to_read:
    with open(output_file, "wb") as tmp_file:

您有一个缩进错误,并且您有混合的空格和制表符。始终使用制表符进行缩进。

最新更新