186π
Itβll be good to see the csv file itself, but this might work for you, give it a try, replace:
file_read = csv.reader(self.file)
with:
file_read = csv.reader(self.file, dialect=csv.excel_tab)
Or, open a file with universal newline mode
and pass it to csv.reader
, like:
reader = csv.reader(open(self.file, 'rU'), dialect=csv.excel_tab)
Or, use splitlines()
, like this:
def read_file(self):
with open(self.file, 'r') as f:
data = [row for row in csv.reader(f.read().splitlines())]
return data
55π
I realize this is an old post, but I ran into the same problem and donβt see the correct answer so I will give it a try
Python Error:
_csv.Error: new-line character seen in unquoted field
Caused by trying to read Macintosh (pre OS X formatted) CSV files. These are text files that use CR for end of line. If using MS Office make sure you select either plain CSV format or CSV (MS-DOS). Do not use CSV (Macintosh) as save-as type.
My preferred EOL version would be LF (Unix/Linux/Apple), but I donβt think MS Office provides the option to save in this format.
- [Django]-How to merge consecutive database migrations in django 1.9+?
- [Django]-What's the point of Django's collectstatic?
- [Django]-How to deal with "SubfieldBase has been deprecated. Use Field.from_db_value instead."
33π
For Mac OS X, save your CSV file in βWindows Comma Separated (.csv)β format.
- [Django]-Removing 'Sites' from Django admin page
- [Django]-How to solve "Page not found (404)" error in Django?
- [Django]-How to completely dump the data for Django-CMS
19π
If this happens to you on mac (as it did to me):
- Save the file as
CSV (MS-DOS Comma-Separated)
-
Run the following script
with open(csv_filename, 'rU') as csvfile: csvreader = csv.reader(csvfile) for row in csvreader: print ', '.join(row)
- [Django]-Testing nginx without domain name
- [Django]-How to get GET request values in Django?
- [Django]-Django β No module named _sqlite3
- [Django]-Django: Grab a set of objects from ID list (and sort by timestamp)
- [Django]-FileUploadParser doesn't get the file name
- [Django]-How can I test https connections with Django as easily as I can non-https connections using 'runserver'?
2π
This is an error that I faced. I had saved .csv file in MAC OSX.
While saving, save it as βWindows Comma Separated Values (.csv)β which resolved the issue.
- [Django]-Django filter queryset __in for *every* item in list
- [Django]-Django-way for building a "News Feed" / "Status update" / "Activity Stream"
- [Django]-Having Django serve downloadable files
1π
This worked for me on OSX.
# allow variable to opened as files
from io import StringIO
# library to map other strange (accented) characters back into UTF-8
from unidecode import unidecode
# cleanse input file with Windows formating to plain UTF-8 string
with open(filename, 'rb') as fID:
uncleansedBytes = fID.read()
# decode the file using the correct encoding scheme
# (probably this old windows one)
uncleansedText = uncleansedBytes.decode('Windows-1252')
# replace carriage-returns with new-lines
cleansedText = uncleansedText.replace('\r', '\n')
# map any other non UTF-8 characters into UTF-8
asciiText = unidecode(cleansedText)
# read each line of the csv file and store as an array of dicts,
# use first line as field names for each dict.
reader = csv.DictReader(StringIO(cleansedText))
for line_entry in reader:
# do something with your read data
- [Django]-Best practices for getting the most testing coverage with Django/Python?
- [Django]-How do I do an OR filter in a Django query?
- [Django]-Django models: get list of id
1π
I know this has been answered for quite some time but not solve my problem. I am using DictReader and StringIO for my csv reading due to some other complications. I was able to solve problem more simply by replacing delimiters explicitly:
with urllib.request.urlopen(q) as response:
raw_data = response.read()
encoding = response.info().get_content_charset('utf8')
data = raw_data.decode(encoding)
if '\r\n' not in data:
# proably a windows delimited thing...try to update it
data = data.replace('\r', '\r\n')
Might not be reasonable for enormous CSV files, but worked well for my use case.
- [Django]-DatabaseError: current transaction is aborted, commands ignored until end of transaction block?
- [Django]-Django unit tests without a db
- [Django]-Django CSRF check failing with an Ajax POST request
0π
Alternative and fast solution : I faced the same error. I reopened the βwierdβ csv file in GNUMERIC on my lubuntu machine and exported the file as csv file. This corrected the issue.
- [Django]-OperationalError: database is locked
- [Django]-Cross domain at axios
- [Django]-How to show processing animation / spinner during ajax request?