0

This is my code:

filepath = sys.argv[1]

csvdata = list(csv.reader(open(filepath)))

How can I fix it?

I saved my excel file as a csv and receieved this error:

_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
9
  • 1
    A billion ways, first include the stack trace and a bit more info on your code! Commented Aug 8, 2017 at 17:51
  • it's probably not a valid CSV file. You have to filter out nul bytes at best. Commented Aug 8, 2017 at 17:53
  • Are nul bytes empty cells? I am using an excel file. Commented Aug 8, 2017 at 17:56
  • 1
    if you are using an Excel file, try first exporting it/saving it as a csv file. An Excel worksheet is not a csv file. Commented Aug 8, 2017 at 18:11
  • Okay did that, now I got this error "_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?" Commented Aug 8, 2017 at 18:14

3 Answers 3

1
  1. An Excel file is not a csv file. First export / save the file as csv.
  2. There are differences between python versions about whether to open the file as binary or text. This has relevance to how newlines are handled. In Python 2.x, open as binary: open(filepath, 'rb')

    In Python 3.x, don't : open('file.csv', 'r')

    The second part I learned from this link about reading in csv files

  3. For some operating systems (Mac OS for sure) you need to open with the mode 'rU' See: this link with same problem specifically on Mac OS

Sign up to request clarification or add additional context in comments.

10 Comments

I am still getting this error: "_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?"
I think you are going to have to give us some more information. Python version. OS. Example text that shows the problem (preferably a few lines which reproduce the problem. Which of the suggestions above did you try? Did they change the results?
I've tried this here (Linux, Python 2.7) with a csv file of my own making and had no problem. To understand the problem we will need more information
Using Mac OS and I have attempted all solutions given so far.
Yes, I think you need the universal new line mode. See: stackoverflow.com/questions/6726953/…
|
0

try this (put actual location of csv file)...

with open('c:\pytest.csv', 'rb') as csvfile:

    data = csv.reader(csvfile)

    mylist = list (data)

    print mylist

4 Comments

But I need to use this script for a bunch of csv files so I want to make it more universal
That was more of an illustration how to read the CSV file. You can take the standard input of any valid CSV file and apply same logic.
Getting this error: "mylist = list(data) _csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?"
refer below code - opens dialog box to select the file to be read
0
from tkFileDialog import askopenfilename  
import csv

filename = askopenfilename()  
with open(filename, 'rb') as csvfile:  
    data = csv.reader(csvfile)  
    mylist = list (data)  
print mylist

1 Comment

Code dumps without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. See How to Answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.