0

I am currently working on a code for a perceptron that plots data in three dimensions. I am running it in terminal and when running the code I recieve this error:

    Traceback (most recent call last):
File "perceptron2.py", line 70, in <module>
for row in csv_reader:
_csv.Error: new-line character seen in unquoted field - do you need to open the 
file in universal-newline mode?

The area around line 70 of my code, where I believe the error is occuring is in the following block of code, in where input data is acquired from x and labels from y

X = np.zeros(shape=(80,2))
y = np.zeros(shape=(80,1))
z = np.zeros(shape=(80,0))
with open('joy.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        X[line_count] = [eval(row[0]), eval(row[1])]
        y[line_count] = [eval(row[2])]
        z[line_count] = [eval(row[3])]
        line_count += 1
        if line_count == 80:
            break

Anyway, I was wondering if anybody knew of a solution I could implement to solve my problems. Thanks!

Torxed
  • 22,866
  • 14
  • 82
  • 131
Sophia
  • 11
  • 3
  • Does this answer your question? https://stackoverflow.com/questions/6726953/open-the-file-in-universal-newline-mode-using-the-csv-django-module – Collin Phillips Feb 27 '19 at 19:49
  • unfortunately not, i already looked at it but thank you – Sophia Feb 27 '19 at 19:52
  • `with open('joy.csv', 'rU')` should do the trick. If it doesn't work, please state why. The fastest approach after that is that you "simply" read X amount of buffer at a time and try to locate `\r`, `\n` or `\r\n` manually. And you take the position furthest away from the starting point without going past any of the characters - and you split the data into a line and a remainder. Then you keep on doing this, appending to the remainder until it hits the jackpot again. This is manual and tedious, but it would get the job done. But I'd investigate why `rU` isn't working to begin with. – Torxed Feb 27 '19 at 20:31
  • One reason might be that python was compiled with `--without-universal-newlines` for whatever reason. – Torxed Feb 27 '19 at 20:32
  • Sorry for the spam, but just learned myself that this feature is depreciated since Python 3.2. Instead, you have `with open('joy.csv', newline=None)` (universal line ending). Does this help at all? – Torxed Feb 27 '19 at 20:39
  • Thank you! I put "with open('joy.csv', newline=None)" at the bottom of my block of code, and it returned the error invalid syntax @Torxed – Sophia Feb 28 '19 at 19:38
  • I realize, I placed the line in the wrong place at the bottom of my code. When I instead replace with "open('joy.csv') as csv_file:" with "with open('joy.csv', newline=None)" it returns the following error "TypeError: 'newline' is an invalid keyword argument for this function" @Torxed – Sophia Feb 28 '19 at 19:44
  • Which python version are you running? Python2? Extremely important here. – Torxed Feb 28 '19 at 19:52
  • Python 2.7.15 @Torxed – Sophia Mar 01 '19 at 19:33
  • There you go, I don't remember how Python2 behaves. It's end of life in a year as well, so I suggest you try with Python3 and see if it works. The reason why you get `invalid keyword` is because this was added in Python3.3 (I think it was 3.3, some v3 at least). – Torxed Mar 01 '19 at 20:51
  • Thank you for all of your help! However, I updated to Python 3.7 and it returns the same error, that newline is an invalid keyword argument. @Torxed – Sophia Mar 05 '19 at 20:02
  • You're welcome. But I doubt you're starting the correct Python version, if you start python - what version does it say it is? Here's an example of it working in my Python version 3: https://i.imgur.com/azVl3yI.png *(it says Python3 at the top, as verification)* – Torxed Mar 05 '19 at 20:11

0 Answers0