Can not open csv file in JupyterLab - python

I'm getting a file not found error in JupyterLab. I can see the file and I have tried several different ways to open it via JupyterLab, but I keep getting an error as if the file does not exist.
Below is my code:
import os
import csv
csvpath = os.path.join('..','Resources','budget_data.csv')
with open(csvpath, newline="") as csvfile:
`enter code here`csvreader = csv.reader(csvfile, delimiter=",")
Here is the error:
FileNotFoundError Traceback (most recent call last)
<ipython-input-17-3c32e2cc9d43> in <module>
----> 1 with open(csvpath, newline="") as csvfile:
2 csvreader = csv.reader(csvfile, delimiter=",")
FileNotFoundError: [Errno 2] No such file or
directory: '..,Resources,budget_data.csv'
Here is the path: GTATL201902DATA3/03-Python/Homework/Instructions/PyBank/Resources/budget_data.csv

csvpath = os.path.join('..','Resources','budget_data.csv')
Are you absolutely sure this is the actual code you're using? Because going by the error message, it looks like you did this instead:
csvpath = os.path.join('..,Resources,budget_data.csv')
i.e. the .. and the commas are literally part of the string, instead of joining three separate strings.

Related

Read Write mode Python CSV file

Working on a project, but I wanted read and write mode when I opened the csv file. Sadly, it is giving me an error. Here is the code:
import csv
with open("historicalData/ACAD.csv", "rw") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for lines in csv_reader:
print(lines[0])
And the error I am getting:
Traceback (most recent call last):
File "c:/Users/sande/Desktop/Vihaan/ThirdPartySoftware/Python/VisualStudiosCode/DadWork/test.py", line 3, in <module>
with open("historicalData/ACAD.csv", "ra") as csv_file:
ValueError: must have exactly one of create/read/write/append mode
Can anyone help in how to achieve this?
Use r+ or a+ or w+ not rw as python doesn't recognise it

csv.field_size_limit not worked

why this error cant be handled? i already tried all of solution in stackoverflow about this problem using field_size_limit, but not worked. here is my code
import sys
import csv
csv.field_size_limit()
with open('data_latih_jokowi_negatif.csv', 'r', encoding="latin-1") as file:
readCSV = list(csv.reader(file))
#del(readCSV[1664], readCSV[1663])
progress = 0
result = readCSV
and this is the error
Traceback (most recent call last):
File "cleandoublesentences.py", line 7, in <module>
readCSV = list(csv.reader(file))
_csv.Error: field larger than field limit (131072)
You will need to call field_size_limit() with the new limit.
csv.field_size_limit(10000000) # 10 megabytes, give or take

IO error when reading csv files in python

I have the following code:
for file in os.listdir('/home/sainik/Final/'+str(folderno)):
if file.endswith('.csv'):
print file
with open(file,'rb') as csvfile:
spamreader = csv.reader(csvfile)
for row in spamreader:
print row
when running the code, I am getting the following error:
Traceback (most recent call last):
File "/home/sainik/Final/Programs/sainik.py", line 28, in <module>
with open(file,'rb') as csvfile:
IOError: [Errno 2] No such file or directory: '4.csv'
Kindly help.
You are trying to open the file from the path you are running the script.
You should try to open the full path
with open('/home/sainik/Final/' + file)
You are passing only the filename for open function. You should pass path to the open function. Two possible ways to pass the path of file to open function, either relative path or full path.
try:
with open( os.path.join('/home/sainik/Final/',str(folderno),file),'rb') as csvfile:
Your script is looking at it's own directory for file 4.csv.
Try it like this:
for file in os.listdir('/home/sainik/Final/'+str(folderno)):
if file.endswith('.csv'):
print file
with open(/home/sainik/Final/'+str(folderno)+'\/'+file,'rb') as csvfile:
spamreader = csv.reader(csvfile)
for row in spamreader:
print row

Error during opening csv file in Python

I saved csv file in (C:/Users/kgu/PycharmProjects/untitled1/test1.py). named as testdata.csv
my code is :
import csv
f=open('testdata.csv','r',encoding='utf-8')
And the error message is here :
C:\Users\kgu\Anaconda3\python.exe
C:/Users/kgu/PycharmProjects/untitled1/test1.py
Traceback (most recent call last):
File "C:/Users/kgu/PycharmProjects/untitled1/test1.py", line 3, in <module>
f=open('testdata.csv','r',encoding='utf-8')
FileNotFoundError: [Errno 2] No such file or directory: 'testdata.csv'
Process finished with exit code 1
I did save the testdata.csv in the python project folder.
I don't know what is wrong in it.
If you use Pandas, you can try:
import pandas as pd
filepath = "/yourfilepath/yourfile.csv"
# check your sep (";" , "," ...)
f = pd.read_csv(filepath, encoding="utf-8", sep="yoursep")
You could do it like this - if you are in the right working directory:
with open(testdata.csv', 'rb') as csvfile:
data = [x for x in csv.reader(csvfile, delimiter='yourdelimiter')]

a bug with csv module in python

import csv
with open('database.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row['NAME'])
Guys, I have a csv file with the first row as a index, and in linux the code read from row['NAME'] and print only the names form colum NAME, when I run it in windows, it says:
C:\Users\Desktop>python py.py
Traceback (most recent call last):
File "py.py", line 5, in <module>
print(row['NAME'])
KeyError: 'NAME'
WHY?
If you are using the python 2- versions, you need to open the csv with rb, i.e.:
with open('database.csv', 'rb') as csvfile:....
for reference, checkout https://docs.python.org/2/library/csv.html as it includes a part in the reader doc about this.

Categories