python


how to load csv data in Python 3 using Jupyter


I am trying to import data from a csv file using Jupyter. I am new to Python.
It works fine in IDLE, but not in Jupyter.
import csv
with open("mpg.txt","r") as mpgFile:
mpgFileReader = csv.reader(mpgFile)
mpgList = []
for row in mpgFileReader:
if len (row)!=0:
mpgList = mpgList + [row]
mpgFile.close()
print(mpgList)
This is the error I receive
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-60-42823a11b1d0> in <module>()
1 import csv
----> 2 with open("mpg.txt","r") as mpgFile:
3 mpgFileReader = csv.reader(mpgFile)
4 mpgList = []
5 for row in mpgFileReader:
FileNotFoundError: [Errno 2] No such file or directory: 'mpg.txt'
I also tried to specify the full path of the file:
"C:\Users\serdi\Documents\Test\mpg.txt"
But then it gives me another error:
File "<ipython-input-61-fed3bca4876a>", line 2
with open("C:\Users\serdi\Documents\Test\mpg.txt","r") as mpgFile:
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
The original problem is likely as you surmised, namely the path is incorrect. However the second error is due to the \ in the
"C:\Users\serdi\Documents\Test\mpg.txt" The \ is an escape. There are at least three alternatives.
Use a raw string:
with open(r"C:\Users\serdi\Documents\Test\mpg.txt","r") as mpgFile:
note the r in front of the string.
Escape the \:
with open("C:\\Users\\serdi\\Documents\\Test\\mpg.txt","r") as mpgFile:
Use '/' instead of \:
with open("C:/Users/serdi/Documents/Test/mpg.txt","r") as mpgFile:

Related Links

Unable to handle “HTTP Badstatus line” exception
How to merge/join two resultset/table in django?
Maximise the Slope using CVXPY
Python-Instagram user_recent_media returning next of none
Django - AttributeError: 'module' object has no attribute 'admin'
Convert pandas (python) categorical x-axis to continuous values when plotting
How to make a specific type of python face tracker
OSError when trying to delete keys from redis
remove/reset matplotlib quiver arrows
Repeating elements in a list
Run python-rq worker process on application start
Chrome Not Starting When Using ChromeDriver
How to selectively iterate over files with Python
I'm having trouble using docker-py in a development environment on OSX
How to query AWS to get ELB names and attached instances to that using python boto modules?
Pi Wireless AP is dropping shortly after boot

Categories

HOME
arrays
google-apps-script
cluster-computing
symfony
filterrific
xamarin.android
jsviews
textwatcher
webrtc
google-tag-manager
qpython3
azureservicebus
laravel-4
ios10
lotus-notes
game-maker-studio-1.4
intellij-plugin
sign
typeahead
rhapsody
jsonserializer
zoho
styles
nmake
k2
novnc
tfs2013
shopping-cart
unpack
metadata-extractor
cell-array
encase
outsystems
graph-databases
web-audio-api
nlb
redhat-datavirt
discord.net
android-preferences
floating-accuracy
homekit
pytest-django
cppunit
lumen-5.3
tiddlywiki
maintenance
rpostgresql
execl
communication-protocol
ingres
festival
settimeout
payu
backstop.js
scalar
mercurial-hook
string-parsing
date-format
distribute
spinlock
interactive-brokers
fortran90
portfolio
plone-3.x
optionbutton
enyo
insert-into
eyeql
mirrorlink
lmfit
kendo-menu
playscape
ffprobe
twitter-rest-api
gray-code
swift2.1
git-checkout
cocoascript
getimagedata
bsp
neoload
codeigniter-a3m
hamsterdb
convex-polygon
adomd.net
reporting-tools
vertical-scrolling
mozart-mvc
blockingqueue
sunspot-rails
symfony-2.0
redirectstandardoutput
subtract
supersized
nssavepanel
curljs
windows-live-id
scatterview
microsoft.ink
dirty-data
standardized

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App