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

Building logic gates
Return all tuple only from list
Reassign rows and columns in Python csv
How to check raw_input answers inside if-statements
how does one define a nullary function in python?
Sympy, plotting geometric entities yields either ImportError: no module named 'plot' or AttributeError: 'Circle' object has no attribute 'is_3D'
Select first row that meets certain condition
Splitting longer patterns using regex without losing characters Python 3+
Using python sdist to create a package that includes generated files in the build directory
Any elegant way to add a method to an existing object in python?
Setting cookies for static files using bottle.py
Python/Feedparser: reading RSS feed fails
How to generate a Torrent Magnet URI from a directory?
Python/Kivy camera widget error with opencv
Python3 - When exactly do you need to prepend “self._” to variable declarations within class methods? [duplicate]
how to tell python 3 to skip over non-digit characters from a csv file

Categories

HOME
sharepoint
xbox-live
signalr
websphere
system-verilog
bing
puppet
reportportal
hana
phpstorm
csvhelper
search-engine
specflow
propertygrid
gspread
click
jboss-eap-7
future
medium-editor
netezza
opera-mini
aspell
google-people
propel2
plupload
embedly
prediction
remove-method
azure-servicebus-queues
myob
continuous-deployment
lync-2013
dss
cortex-m3
datadog
vegan
picturebox
postgresql-9.2
read-write
graph-databases
preg-grep
hawtio
body-parser
discord.net
installshield-2012
wsadmin
ssh.net
bing-translator-api
constants
tango
nsjsonserialization
data-integration
walmart-electrode
xmgrace
spell-checking
udev
ruby-on-rails-2
photon-controller
quintus
festival
rsqlite
pdflib
windows-azure-pack
update-site
spatial-query
emokit
gradle-release-plugin
supportmapfragment
anti-cheat
fortran90
cmocka
vips
zuora
evo
fabric-twitter
android-snackbar
paypal-nvp
castle-windsor-3
manjaro
pyopengl
guzzle6
hiera
uptodate
custom-url
rhel5
grunt-contrib-concat
livechat
x-ua-compatible
sid
valence
indexing-service
reserved-words
lov
java.util.date
astyanax
mstsc
digital-design
client-library
awesomeprint
magickwand
phpsh
transitive-closure-table
mozart-mvc
moq-3
redirectstandardoutput
makefile-project
nintendo
tcxgrid
coff
executable-format
pivotal-crm
querypath
communicationexception
google-instant
boost-date-time
iphone-sdk-3.2
openwysiwyg
inline-if
data-entry
ajaxpro

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