在python中导入csv模块时出错

时间:2019-01-05 16:25:32

标签: python

import csv    
with open('test.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)

回溯:

Traceback (most recent call last):   File
   "E:\Python\datatest\csvtest.py", line 1, in <module>
       import csv ImportError: bad magic number in 'csv': b'\x03\xf3\r\n' [Finished in 0.1s with exit code 1] [shell_cmd:
   python -u "E:\Python\datatest\csvtest.py"] [dir: E:\Python\datatest]
   [path:
   C:\Python37-32\;C:\Python37-32\Scripts\;C:\ProgramData\Boxstarter;C:\Program
   Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files
   (x86)\Common Files\Intel\Shared
   Files\cpp\bin\Intel64;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;E:\OneDrive\Nodejs\;C:\Program
   Files\Microsoft SQL
   Server\110\Tools\Binn\;C:\WINDOWS\System32\OpenSSH\;C:\Program
   Files\Microsoft VS Code\bin;C:\Program
   Files\nodejs\;C:\ProgramData\chocolatey\bin;C:\Program
   Files\Git\cmd;C:\Program Files\Amazon\AWSCLI\bin\;C:\Program Files
   (x86)\Google\Cloud
   SDK\google-cloud-sdk\bin;C:\Python37-32\Scripts\;C:\Python37-32\;C:\Users\daunh\AppData\Local\Microsoft\WindowsApps;C:\Program
   Files\Heroku\bin;C:\Users\daunh\AppData\Local\atom\bin;C:\Users\daunh\AppData\Local\Microsoft\WindowsApps;C:\Program
   Files\Microsoft VS Code\bin;C:\Users\daunh\AppData\Roaming\npm]

1 个答案:

答案 0 :(得分:0)

这通常是由过时的.pyc文件引起的。搜索csv.pyc,将其删除,然后重试。