Python 3.2 skip a line in csv.DictReader - python

How do I skip a line of records in a CSV when using a DictReader?
Code:
import csv
reader = csv.DictReader(open('test2.csv'))
# Skip first line
reader.next()
for row in reader:
print(row)
Error:
Traceback (most recent call last):
File "learn.py", line 3, in <module>
reader.next()
AttributeError: 'DictReader' object has no attribute 'next'

You use next(reader) instead.
Source: csv.DictReader documentation

Since Python 2.6 you should use next(foo) instead of foo.next().

It was considered a mistake in python2 to have the method called next() instead of __next__()
next(obj) now calls obj.__next__() just like str, len etc. as it should.
You usually wouldn't call obj.__next__() directly just as you wouldn't call obj.__str__() directly if you wanted the string representation of an object.
Handy to know if you find yourself writing unusual iterators

Related

I keep getting a type error got str instead of int

import pickle
usernames_passwords = open("username_password.pck", "wb")
customer_login = []
pickle.dump(customer_login, usernames_passwords, "wb")
usernames_passwords.close()
I'm trying to dump a list of usernames and passwords into a pickle file, and I keep getting a type error. Can anyone explain what I'm doing wrong?
Traceback (most recent call last):
File "/Users/andy/PycharmProjects/python/venv/scratch.py", line 4, in <module>
pickle.dump(customer_login, usernames_passwords, "wb")
TypeError: an integer is required (got type str)
Process finished with exit code 1
From the pickle docs:
The optional protocol argument, an integer, tells the pickler to use the given protocol; supported protocols are 0 to HIGHEST_PROTOCOL. If not specified, the default is DEFAULT_PROTOCOL. If a negative number is specified, HIGHEST_PROTOCOL is selected.
So, your third argument is using the format you'd use to open a file, but pickle works differently and expects an int. See the docs here.

Why does a 'WriteOnlyWorksheet' object have no attribute 'cell'?

import openpyxl
wb=openpyxl.Workbook("multiplication.xlsx")
wb.create_sheet()
sheet=wb.get_active_sheet()
sheet.cell(column=6, row=4).value= 5
wb.save("multiplication.xlsx")
When i try and write in the cell, I receive this error.
Traceback (most recent call last):
File "/Users/bjg/Desktop/excel2.py", line 8, in <module>
sheet.cell(column=6, row=4).value= 5
AttributeError: 'WriteOnlyWorksheet' object has no attribute 'cell'
I was wondering if anybody knew why this was the case?
From the write-only mode docs:
In a write-only workbook, rows can only be added with append(). It is not possible to write (or read) cells at arbitrary locations with cell() or iter_rows().
Instead of doing:
wb=openpyxl.Workbook("multiplication.xlsx")
just do:
wb=openpyxl.Workbook()
then at last save with:
wb.save("multiplication.xlsx")

'_csv.writer' object has no attribute 'write'

I am not sure what the problem is here. I have a csv file I want to filter. I want to remove all lines starting with '#' and all lines where the third column is the string 'chrM'. Im basically setting my code up to be like the answer here:
TypeError: expected a character buffer object
But Im getting an error.
import re
import csv
inputSamFile = 'excerpt'
outSamFile = 'filternoM'
with open(inputSamFile) as inputSam, open(outSamFile, 'wt') as outSam:
inputSamCont = csv.reader(inputSam, delimiter = '\t')
outSamCont = csv.writer(outSam, delimiter = '\t')
for line in inputSamCont:
if line[0].startswith('#'):
continue
elif line[2] == 'chrM':
continue
else:
outSamCont.write(line)
Traceback (most recent call last):
File "filterMito.py", line 19, in
outSamCont.write(ProcessLine(line))
AttributeError: '_csv.writer' object has no attribute 'write'
What am I doing wrong
You may be looking for .writerow().
I also ran into this problem, as the documentation I was following used .write(), but csv.writer objects use .writerow().
The error tells you everything you need to know.
AttributeError: '_csv.writer' object has no attribute 'write'
In your code, you create the object:
outSamCont = csv.writer(outSam, delimiter = '\t')
then try to call the .write() method:
outSamCont.write(line)
(or, as it is in the traceback
outSamCont.write(ProcessLine(line))
I'm not sure why you have posted different code to what you're running).
However, that object, a csv.writer, does not have the method write, hence the error message. See the documentation for csv.writer objects for the list of methods they do have, and choose the appropriate one.

Create Python array.array Object from cStringIO Object

I want to create an array.array object from a cStringIO object:
import cStringIO, array
s = """
<several lines of text>
"""
f = cStringIO.StringIO(s)
a = array.array('c')
a.fromfile(f, len(s))
But I get the following exception:
Traceback (most recent call last):
File "./myfile.py", line 22, in <module>
a.fromfile(f, len(s))
TypeError: arg1 must be open file
It seems like array.array() is checking the type() of the first argument, which makes it incompatible with cStringIO (and StringIO for that matter). Is there any way to make this work?
Why not use a.fromstring()? Since the StringIO buffer is entirely in memory, there is no benefit to trying to use a file api to read the bits from one memory location to another.
a = array.array('c')
a.fromstring(s)
If you are using StringIO for another reason (as a memory buffer, or as a file earlier on), then you can use StringIO's getvalue() function to get the string value.

Creating a Python function that opens a textfile, reads it, tokenizes it, and finally runs from the command line or as a module

I have been trying to learn Python for a while now. By chance, I happened across chapter 6 of the official tutorial through a Google search link pointing
here.
When I learned, from that page, that functions were the heart of modules, and that modules could be called from the command line, I was all ears. Here's my first attempt at doing both, openbook.py
import nltk, re, pprint
from __future__ import division
def openbook(book):
file = open(book)
raw = file.read()
tokens = nltk.wordpunct_tokenize(raw)
text = nltk.Text(tokens)
words = [w.lower() for w in text]
vocab = sorted(set(words))
return vocab
if __name__ == "__main__":
import sys
openbook(file(sys.argv[1]))
What I want is for this function to be importable as the module openbook, as well as for openbook.py to take a file from the command line and do all of those things to it.
When I run openbook.py from the command line, this happens:
gemeni#a:~/Projects-FinnegansWake$ python openbook.py vicocyclometer
Traceback (most recent call last):
File "openbook.py", line 23, in <module>
openbook(file(sys.argv[1]))
File "openbook.py", line 5, in openbook
file = open(book)
When I try using it as a module, this happens:
>>> import openbook
>>> openbook('vicocyclometer')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
So, what can I do to fix this, and hopefully continue down the long winding path to enlightenment?
Error executing openbook.py
For the first error, you are opening the file twice:
openbook(file(sys.argv[1]))
ph0 = open(book)
Calling both file() and open() is redundant. They both do the same thing. Pick one or the other: preferably open().
open(...)
open(name[, mode[, buffering]]) → file object
Open a file using the file() type, returns a file object. This is the
preferred way to open a file.
Error importing openbook module
For the second error, you need to add the module name:
>>> import openbook
>>> openbook.openbook('vicocyclometer')
Or import the openbook() function into the global namespace:
>>> from openbook import openbook
>>> openbook('vicocyclometer')
Here are some things you need to fix:
nltk.word_tokenize will fail every time:
The function takes sentences as arguments. Make sure that you use nltk.sent_tokenize on the whole text first, so that things work correctly.
Files not being dealt with:
Only open the file once.
You're not closing the file once it's done. I recommend using Python's with statement to extract the text, as it closes things automatically: with open(book) as raw: nltk.sent_tokenize(raw) ...
Import the openbook function from the module, not just the module: from openbook import openbook.
Lastly, you could consider:
Adding things to the set with a generator expression, which will probably reduce the memory load: set(w.lower() for w in text)
Using nltk.FreqDist to generate a vocab & frequency distribution for you.
Try
from openbook import *
instead of
import openbook
OR:
import openbook
and then call it with
openbook.openbook("vicocyclometer")
In your interactive session, you're getting that error because you need to from openbook import openbook. I can't tell what happened with the command line because the line with the error got snipped. It's probably that you tried to open a file object. Try just passing the string into the openbook function directly.

Categories