Relative path not working in Pandas python in Jupyter notebook - python-3.x

my folder structure is :
datasets/file.csv
source/code.ipynb
from within i want to access the file named file.csv.
import pandas as pd
data = pd.read_csv("../datasets/file.csv")
This is giving me the error : ParserError: Error tokenizing data. C error: Expected 1 fields in line 68, saw 2
How to access file using relative path in pandas python?
I am using Python3.6 with Anaconda in Windows 8.1 with Jupyter notebook.

The ParseError indicates that your error is in parsing the file, not locating and opening it. To verify this, try:
test_file = open('../datasets/file.csv')
for line in test_file:
print(line.strip())
This should print out the lines in file.csv.

Related

how to import csv file to anaconds kernel?

I am trying to import csv file to jupyter notebook but there seems to be error, Plz help with correct command.
Command : a=pd.read_csv(r"C:\Users\Priti M\Downloads\NAME LIST 19FEB PC\NAME LIST 19FEB PC.csv")

python throws 'NoduleNotFound' error for a module that I'm not importing

This is in python-3.7.6
I have some .pickle files that store some data I scraped off a website, each file has one snapshot of the data. Now I'm loading those snapshot files and ganging together data to form a timeseries.
I hit one file that is fine in all respects, as far as I can see, but python throws a
*** ModuleNotFoundError: No module named 'bs4'
which I'm not importing or using.
ONe time the file was zero-length. I deleted it and the script ran fine. But this file in question is fine, I can load it in ipython without any problem.
The code is...
import os, sys, re
import optparse as op
import glob
import pickle
import datetime
# and later
for f in files:
data=pickle.load(open(f,'rb') # which throws the error

ModuleNotFoundError: No module named 'numpy.core.multiarray\r'

I am trying to load a pkl file,
pkl_file = open(sys.argv[1], 'rb')
world = pickle.load(pkl_file)
but I get an error from these lines
Traceback (most recent call last):
File "E:/python/test.py", line 186, in <module>
world = pickle.load(pkl_file)
ModuleNotFoundError: No module named 'numpy.core.multiarray\r'
I am using Windows 10, python 3.7, and installed four packages (numpy 1.17.2, opencv-python 4.1.1.26, pip 19.2.3, setuptools 41.2.0 ). I have tried to change "rb" to "r", but still got the error, how can I fix this?
I think there are two problems here.
First, your pickle is or contains a NumPy object, which is not part of the standard library. Therefore you must ensure that NumPy is installed into your current Python environment and imported before you try to load the pickled object. Depending on your setup, installation may be as simple as,
pip install numpy
Then you must add the line,
import numpy as np
to the top of your script.
Second, it looks like Python is encountering this issue, where your binary file was erroneously saved as text on Windows, resulting in resulted in each '\n' being converted to '\r\n'. To fix this, you must re-convert to '\r\n' back to '\n'. So long as the file isn't huge, this usually isn't very painful.
Here is a relatively complete example:
import sys
import numpy as np
src = sys.argv[1] # path to your file
data = open(src).read().replace('\r\n', '\n') # read and replace file contents
dst = src + ".tmp"
open(dst, "w").write(data) # save a temporary file
world = pickle.load(open(dst, "rb"), encoding='latin1')
Ok, I just had to figure this out for myself, and I solved it. All you have to do is change all the "\r\n" to "\n". You can do this in multiple ways. You can go into Notepad++ and change line endings from CR LF to just LF. Or programmatically you can do
open(newfile, 'w', newline = '\n').write(open(oldfile, 'r').read())

Trying to import a .csv file in pandas using python. Getting Unicode Decode error

I am trying to import a .csv file into pandas but I am getting a Unicode error. I am running a windows pc.
I am using the following command:
medals =pd.read_csv('C:\\Users\\Username\\Downloads\\data\\olympicmedals.csv')
What am I missing here?
This is just to import a .csv file into my notebook
I am using the following command:
medals =pd.read_csv('C:\\Users\\Username\\Downloads\\data\\olympicmedals.csv')
The file should be imported into my Jupyter notebook
Try this
medals =pd.read_csv(r"C:\\Users\\Username\\Downloads\\data\\olympicmedals.csv")

Read csv file Anaconda | Python 3

I am trying to open a CSV file on Anaconda (Python 3) unsuccessfully.
I tried with a raw string, I included the whole path and also tried with the double backslashes but nothing worked. I still get the Errno 2 No such file or directory.
This is my code:
reader = csv.reader(open(r'C:\Users\Marco\Desktop\trainingset.csv', newline=''), delimiter=' ', quotechar='|')
for row in reader:
print(", ",join(row))
I have the same issue when trying to open a csv file this way... I don't know the reason but instead, I use the pandas library that has a method named read_csv()
pandas.read_csv('myfile.csv')
It gets the content of your csv file as a dataframe object. This works with Python 3.5 using Anaconda3.

Resources