【问题标题】:I didn't find my erroe anywhere我在任何地方都没有找到我的错误
【发布时间】:2018-11-05 16:58:24
【问题描述】:

嗨,有人能告诉我为什么我在最后一行出现的语法无效,正好在第二个“ 这是一个遍历 CSV 文件并将所有内容转换为十进制 ASCII 等值的脚本。谢谢

import csv
with open('C:\Users\user\Desktop\log_transformed_truncated.csv', 'w') as outfile:
c = csv.writer(outfile)
counter = 0
with open('C:\Users\user\Desktop\log.csv','rb') as f:
    for row in csv.reader(f, delimiter=';'):
        counter = counter + 1
        mod_row = []
        if counter != 1:
            for cell in row:
                mod_row.append(sum(bytearray(cell)))
        else:
            for cell in row:
                mod_row.append(cell)
        c.writerow(mod_row)
print "processed {} rows".format(counter)

【问题讨论】:

  • 缩进错误?
  • 这是 python 2 还是 3?
  • 你能把整个错误信息贴出来吗?
  • 上方with下的整个块,从c = csv.writer(outfile)开始应该缩进。

标签: python csv error-handling


【解决方案1】:

你忘了放括号,因为它是 python 3 的语法。

这句话对我有用:

print("processed {} rows".format(counter)) 

您还可以查看此answer,了解有关 python 2 和 python 3 中打印语句之间差异的更多信息。

【讨论】:

  • ...假设提问者使用的是 python 3。
  • 如果您在 python 3 IDE 中运行 print "processed {} rows".format(counter) 行,您将在第二个 " 处收到预期的语法错误,就像提问者提到的那样,让我相信他正在使用 python 3 .@tdelaney
猜你喜欢
  • 2022-11-02
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-09
相关资源
最近更新 更多