a bug with csv module in python - 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.

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

Can not open csv file in JupyterLab

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.

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

Python reading from CSV file in python 2.7 but not python 3.6. What do i have to do to make it work in 3.6

I have some code which i coded in python 2.7, however I need it to work for 3.6 and when i run it i get this error and i am not sure why.
import csv
def ReadFromFile():
with open('File.csv', 'r') as File:
cr = csv.reader(File)
for row in cr:
Name = row[0]
Gender = row[1]
print(Name + Gender)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
ReadFromFile()
File "F:/Test.py", line 6, in ReadFromFile
Name = row[0]
IndexError: list index out of range
I am using the same code saved on a memory stick with the file in 2.7 i get my desired out come of it being read but in 3.6 i am stuck with the error. Thanks for any help
Edit: Added Print
After adding print i got
ELIZABETHFemale
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
ReadFromFile()
File "F:/Test.py", line 6, in ReadFromFile
Name = row[0]
IndexError: list index out of range
So it gave me the first line but nothing more
Python's CSV module has changed how it wants the files you pass to it to be opened. You want to avoid the file object doing any newline transformation because some CSV formats allow embedded newlines within quoted fields. The csv module will do its own newline normalization, so the usual universal newline handling the file object does is redundant.
This is mentioned in the csv.reader documentation, where it is talking about the file argument:
If csvfile is a file object, it should be opened with newline=''.
So for your code, try changing open('File.csv', 'r') to open('File.csv', 'r', newline='').
Have you tried pandas?
I think you may want to use something like
import pandas as pd
def ReadFromFile():
df = pd.read_csv('File.csv')
for row in df:
Name = row[0]
Gender = row[1]
print(Name + Gender)

CSV read error: new-line character seen in unquoted field

I created a python script which works with a test CSV dataset of 10 records. When I scaled this up to the actual datasets (a few thousand rows), I am getting the following error:
_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
The code is as follows:
with open('./Origins.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)
origincoords = ['{Y},{X}'.format(**row) for row in reader]
The full error code is:
Traceback (most recent call last):
File "./Driving.py", line 14, in <module>
origincoords = ['{Y},{X}'.format(**row) for row in reader]
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/csv.py", line 103, in next
self.fieldnames
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/csv.py", line 90, in fieldnames
self._fieldnames = self.reader.next()
_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
Perhaps there is a scale problem with the CSV reading method I am using?
From PEP-0278:
In a Python with universal newline support open() the mode parameter
can also be "U", meaning "open for input as a text file with universal
newline interpretation". Mode "rU" is also allowed, for symmetry with
"rb"
So try to change
with open('./Destinations.csv', 'r') as csvfile:
to
with open('./Destinations.csv', 'rb') as csvfile:
If the error persists, change to
with open('./Destinations.csv', 'rU') as csvfile:
Edited accorded to Martijn Pieters's comment.

Categories