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?
user8233898
  • 43
  • 1
  • 8

3 Answers3

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

Basya
  • 1,477
  • 1
  • 12
  • 22
  • 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?" – user8233898 Aug 08 '17 at 18:39
  • 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? – Basya Aug 08 '17 at 19:05
  • 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 – Basya Aug 08 '17 at 19:35
  • Using Mac OS and I have attempted all solutions given so far. – user8233898 Aug 08 '17 at 19:35
  • Yes, I think you need the universal new line mode. See: https://stackoverflow.com/questions/6726953/open-the-file-in-universal-newline-mode-using-the-csv-django-module – Basya Aug 08 '17 at 19:38
  • Has this helped you? – Basya Aug 09 '17 at 12:54
  • What is customer bulk? I have to be able to use this script for any csv it gets – user8233898 Aug 09 '17 at 14:03
  • Not sure what you are referring to by "customer bulk". If answers here helped you, mark the answer that solved your problems as "accepted" (checkbox top left of answer). You can upvote any post that helped at all, even more than one, but you can only accept one as the answer. – Basya Aug 09 '17 at 14:20
  • In the example the person uses customerbulk, is that a file? I know how this website works, but none of the answers have solved the problem so far which is why I haven't accepted anything as an answer. – user8233898 Aug 09 '17 at 14:23
  • Thank you for marking the answer. I don't know what the customerbulk thing is, I guess some library or module he is using. I linked to it because of the answer about 'rU' and discussion of that, and MacOS is mentioned there as well. What is still not working? You say "I have to be able to use this script for any csv it gets" and you say "I have to be able to use this script for any csv it gets" so I am a bit confused....do you still need help with something? – Basya Aug 09 '17 at 14:41
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
GreenMatt
  • 18,244
  • 7
  • 53
  • 79
0
from tkFileDialog import askopenfilename  
import csv

filename = askopenfilename()  
with open(filename, 'rb') as csvfile:  
    data = csv.reader(csvfile)  
    mylist = list (data)  
print mylist
  • 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 [answer]. – ChrisGPT was on strike Mar 23 '21 at 22:19