0
私はUdemyの学習プロジェクトは
と呼ばれる追跡 "のPythonの30日|あなたのPythonは潜在的なロック解除" そして、これがここDay16のpython3 CSV TypeError例外とValueError _csv.Error
のcoureseが私のコード
ですfilename = "data.csv"
temp_file = NamedTemporaryFile(delete=False)
with open(filename, "rb") as csvfile, temp_file:
reader = csv.DictReader(csvfile)
fieldnames = ["id", "name", "email", "amount", "sent"]
writer = csv.DictWriter(temp_file, fieldnames=fieldnames)
#writer.writeheader()
for row in reader:
print(row)
writer.writerow({
"id": row["id"],
"name": row["name"],
"email": row["email"],
"amount": "1293.23",
"sent": ""
})
私は私のコードは
Traceback (most recent call last):
File "hungry_data.py", line 36, in <module>
for row in reader:
File "C:\Users\Another\AppData\Local\Programs\Python\Python35-32\lib\csv.py", line 109, in __next__
self.fieldnames
File "C:\Users\Another\AppData\Local\Programs\Python\Python35-32\lib\csv.py", line 96, in fieldnames
self._fieldnames = next(self.reader)
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
教師のコードと同じであることを確認しても、私はエラーを取得しますrefore私はそれはライン36ようです.... "R" に "RB" からwith open(filename, "r") as csvfile, temp_file:
を、コードを変更し、このあまりにも
with open(filename, "r", newline='') as csvfile, temp_file:
しかし
Traceback (most recent call last):
File "hungry_data.py", line 43, in <module>
"sent": ""
File "C:\Users\Another\AppData\Local\Programs\Python\Python35-32\lib\csv.py", line 153, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
File "C:\Users\Another\AppData\Local\Programs\Python\Python35-32\lib\tempfile.py", line 483, in func_wrapper
return func(*args, **kwargs)
TypeError: a bytes-like object is required, not 'str'
同じエラーを取得します。行と競合しているどうすればいいですか?