the following code worked until today when I imported from a Windows machine and got this error:
new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
import csv
class CSV:
def __init__(self, file=None):
self.file = file
def read_file(self):
data = []
file_read = csv.reader(self.file)
for row in file_read:
data.append(row)
return data
def get_row_count(self):
return len(self.read_file())
def get_column_count(self):
new_data = self.read_file()
return len(new_data[0])
def get_data(self, rows=1):
data = self.read_file()
return data[:rows]
How can I fix this issue?
def upload_configurator(request, id=None):
"""
A view that allows the user to configurator the uploaded CSV.
"""
upload = Upload.objects.get(id=id)
csvobject = CSV(upload.filepath)
upload.num_records = csvobject.get_row_count()
upload.num_columns = csvobject.get_column_count()
upload.save()
form = ConfiguratorForm()
row_count = csvobject.get_row_count()
colum_count = csvobject.get_column_count()
first_row = csvobject.get_data(rows=1)
first_two_rows = csvobject.get_data(rows=5)
CSV new-line character seen in unquoted field error import csv: Questions
Python import csv to list
1 answers
I have a CSV file with about 2000 records.
Each record has a string, and a category to it:
This is the first line,Line1
This is the second line,Line2
This is the third line,Line3
I need to read this file into a list that looks like this:
data = [("This is the first line", "Line1"),
("This is the second line", "Line2"),
("This is the third line", "Line3")]
How can import this CSV to the list I need using Python?
Answer #1
Using the csv module:
import csv
with open("file.csv", newline="") as f:
reader = csv.reader(f)
data = list(reader)
print(data)
Output:
[["This is the first line", "Line1"], ["This is the second line", "Line2"], ["This is the third line", "Line3"]]
If you need tuples:
import csv
with open("file.csv", newline="") as f:
reader = csv.reader(f)
data = [tuple(row) for row in reader]
print(data)
Output:
[("This is the first line", "Line1"), ("This is the second line", "Line2"), ("This is the third line", "Line3")]
Old Python 2 answer, also using the csv
module:
import csv
with open("file.csv", "rb") as f:
reader = csv.reader(f)
your_list = list(reader)
print your_list
# [["This is the first line", "Line1"],
# ["This is the second line", "Line2"],
# ["This is the third line", "Line3"]]
CSV new-line character seen in unquoted field error open: Questions
How can I open multiple files using "with open" in Python?
5 answers
I want to change a couple of files at one time, iff I can write to all of them. I"m wondering if I somehow can combine the multiple open calls with the with
statement:
try:
with open("a", "w") as a and open("b", "w") as b:
do_something()
except IOError as e:
print "Operation failed: %s" % e.strerror
If that"s not possible, what would an elegant solution to this problem look like?
Answer #1
As of Python 2.7 (or 3.1 respectively) you can write
with open("a", "w") as a, open("b", "w") as b:
do_something()
In earlier versions of Python, you can sometimes use
contextlib.nested()
to nest context managers. This won"t work as expected for opening multiples files, though -- see the linked documentation for details.
In the rare case that you want to open a variable number of files all at the same time, you can use contextlib.ExitStack
, starting from Python version 3.3:
with ExitStack() as stack:
files = [stack.enter_context(open(fname)) for fname in filenames]
# Do something with "files"
Most of the time you have a variable set of files, you likely want to open them one after the other, though.
open() in Python does not create a file if it doesn"t exist
5 answers
What is the best way to open a file as read/write if it exists, or if it does not, then create it and open it as read/write? From what I read, file = open("myfile.dat", "rw")
should do this, right?
It is not working for me (Python 2.6.2) and I"m wondering if it is a version problem, or not supposed to work like that or what.
The bottom line is, I just need a solution for the problem. I am curious about the other stuff, but all I need is a nice way to do the opening part.
The enclosing directory was writeable by user and group, not other (I"m on a Linux system... so permissions 775 in other words), and the exact error was:
IOError: no such file or directory.
Answer #1
You should use open
with the w+
mode:
file = open("myfile.dat", "w+")
Difference between modes a, a+, w, w+, and r+ in built-in open function?
5 answers
In the python built-in open function, what is the exact difference between the modes w
, a
, w+
, a+
, and r+
?
In particular, the documentation implies that all of these will allow writing to the file, and says that it opens the files for "appending", "writing", and "updating" specifically, but does not define what these terms mean.
Answer #1
The opening modes are exactly the same as those for the C standard library function fopen()
.
The BSD fopen
manpage defines them as follows:
The argument mode points to a string beginning with one of the following
sequences (Additional characters may follow these sequences.):
``r"" Open text file for reading. The stream is positioned at the
beginning of the file.
``r+"" Open for reading and writing. The stream is positioned at the
beginning of the file.
``w"" Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.
``w+"" Open for reading and writing. The file is created if it does not
exist, otherwise it is truncated. The stream is positioned at
the beginning of the file.
``a"" Open for writing. The file is created if it does not exist. The
stream is positioned at the end of the file. Subsequent writes
to the file will always end up at the then current end of file,
irrespective of any intervening fseek(3) or similar.
``a+"" Open for reading and writing. The file is created if it does not
exist. The stream is positioned at the end of the file. Subse-
quent writes to the file will always end up at the then current
end of file, irrespective of any intervening fseek(3) or similar.