1
with open('repo-attributes.csv', 'rb') as repofile:
    reader = csv.DictReader(repofile)
    for repo in reader:
        g.add_vertex(name=repo['repository_url'],
            label=repo['repository_url'][19:],
            language='(unknown)' if repo['repository_language'] == 'null'
                else repo['repository_language'],
            watchers=int(repo['repository_watchers']))

This is my code. I am getting the error like as follows. I am new to python. kindly explain this.

Traceback (most recent call last):
  File "C:\Python34\github-network-analysis-master\process.py", line 9, in <module>
    for repo in reader:
  File "C:\Python34\lib\csv.py", line 109, in __next__
    self.fieldnames
  File "C:\Python34\lib\csv.py", line 96, in fieldnames
    self._fieldnames = next(self.reader)
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
3

3 Answers 3

3

Remove b, you are opening in binary mode hence the bytes error:

 with open('repo-attributes.csv', newline="") as repofile:

You can actually remove both as the default mode is r.

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

4 Comments

The OP should add newline='' as well. See the csv.reader() documentation. They most likely read posts about using csv.reader() with Python 2, where using 'rb' is recommended.
@MartijnPieters, I don't see anything related to newline when dealing with DictReader
@MartijnPieters, what is the deal with newline="" I don't ever remember using it? Basically universal newlines?
How can I stop Python's csv.DictWriter.writerows from adding empty lines between rows in Windows? Yes, newline='' stops the file object from translating newlines for you, so the CSV reader can distinguish between embedded newlines and row separators.
0

You are opening file in rb means read binary please open it in read mode. change your code to

...
...
with open('repo-attributes.csv', 'r')...
...
...

This will open file in read mode (not binary).

Comments

0

The last line of the trace tells where the error first occurred. Since you opened the file in binary mode, python is reading bytes. Your file is csv and opening it in read mode will suffice.

So instead of rb use r

Comments

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.