-2

I'm getting a valueError on the below code.

ValueError('too many values to unpack',)
with open('38374657484839373636.csv') as f:
    for line in f.read().split('\n'):
        if line:
            repo, file, pkey = line.split(",")
            keys.add(pkey)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    Evidently there are more than two commas in at least one of your lines. Have you considered using Python's built in CSV handling module? – jonrsharpe Apr 09 '19 at 07:52
  • 1
    Possible duplicate of [Split a string only by first space in python](https://stackoverflow.com/questions/30636248/split-a-string-only-by-first-space-in-python), just use `split(... maxsplit=n)` argument. And if you want to see a more complicated answer, see [this](https://stackoverflow.com/a/21254804/202229) – smci Apr 09 '19 at 07:56
  • Thanks, got it now - apologies didn't see the other thread. – Tensorinsights Apr 09 '19 at 09:29

1 Answers1

0

The easiest way to read CSV-files (and Excel-files too with pd.read_excel()) is to use Pandas

import pandas as pd
df = pd.read_csv('38374657484839373636.csv')
df.head()

Sometimes it is necessary to set the delimiter explicitly, depending on what's in our input file. E.g, if the delimiter in your file is /

import pandas as pd
df = pd.read_csv('38374657484839373636.csv', delimiter='/')
df.head()

Watch also wether your csv-file has a header or not. E.g.:

import pandas as pd
df = pd.read_csv('38374657484839373636.csv', delimiter='/',header=None)
df.head()
pyano
  • 1,885
  • 10
  • 28