pytorch 无法从“torchtext.utils”导入名称“unicode_csv_reader”

ar7v8xwq  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(488)
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-25-b2b2b2ad7bcb> in <cell line: 12>()
     10 from torch.utils.data import Dataset, DataLoader
     11 
---> 12 import pytorch_lightning as pl
     13 import seaborn as sns
     14 from pylab import rcParams

13 frames
/usr/local/lib/python3.9/dist-packages/torchtext/legacy/data/dataset.py in <module>
     11 from torchtext.data.utils import RandomShuffler
     12 from .example import Example
---> 13 from torchtext.utils import download_from_url, unicode_csv_reader
     14 
     15 

ImportError: cannot import name 'unicode_csv_reader' from 'torchtext.utils' (/usr/local/lib/python3.9/dist-packages/torchtext/utils.py)

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

我正在实现一个youtube视频教程。我也这样做,但它不起作用
https://www.youtube.com/watch?v=PCgrgHgy26c
Torch :1.8.1 texttorch:0.9.1 Pytroch-lightning:1.2.6
请帮助我了解问题:(

3mpgtkmj

3mpgtkmj1#

问题出在你的torchtext版本上。
函数unicode_csv_reader在torchtext的0.9.1版本中不再可用。
要读取unicode编码的csv,你可以使用csv库,使用utf-8编码的open打开你的csv文件,然后加载你的数据。

import csv

with open("my_text_data.csv", "r", encoding="utf-8") as f:
    my_text_data = csv.reader(f)

相关问题