【发布时间】:2013-12-08 16:26:28
【问题描述】:
我的 with 语句有语法错误。我想不通。我正在使用 python 3,每次我使用 with 语句时它都不会拥有它。我正在尝试制作一个 txt 文件的阅读器,我需要这个 with 声明。
import csv
value1 = "Spam"
fname = input(open("Please enter the name of the file you wish to open: ")
with open(fname, 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:
if value1 in row:
print(" ".join(row))
我对 python 很陌生,它可能非常明显地为帮助家伙欢呼
【问题讨论】:
-
旁白:如果你在 Python 3 中打开一个 csv 文件,你应该使用
open(fname, 'r', newline='')而不是open(fname, 'rb')(这是你在 Python 2 中的做法。)参见 docs .
标签: python python-3.x syntax with-statement