473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Some csv oddities

Three things that seem odd with csv:

Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
import csv
class unix(csv.Dialect): .... delimiter = ':'
.... escapechar = '\\'
.... lineterminator = '\n'
.... quoting = csv.QUOTE_NONE
.... skipinitialspace = False
.... csv.register_dialect('unix', unix) Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "D:\PYTHON23\lib\csv.py", line 39, in __init__
raise Error, "Dialect did not validate: %s" % ", ".join(errors)
_csv.Error: Dialect did not validate: doublequote parameter must be True or
False
Now, it seems to me that QUOTE_NONE makes doublequote meaningless, because
there's no quote character. And csv.writer doesn't write the quotechar
escaped--using the above dialect, cw.writerow(['1', '"', '3']) will write
the raw bytes '1:":3\n'. However, the corresponding csv.reader chokes on
those bytes.
Untested patch (csv.py, line 64):

if self.doublequote not in (True, False):
- errors.append("doublequote parameter must be True or False")
+ if self.quoting != QUOTE_NONE:
+ errors.append("doublequote parameter must be True or
False")

Moving on...

I also can't seem to use my own registered dialects:
class unix(csv.Dialect): .... delimiter = ':'
.... escapechar = '\\'
.... lineterminator = '\n'
.... quoting = csv.QUOTE_NONE
.... skipinitialspace = False
.... doublequote = False #Fine
.... csv.register_dialect('unix', unix)
csv.list_dialects() #Worked ['excel-tab', 'excel', 'unix'] fp = file('csvtest', 'wb')
csv.writer(fp, 'unix') #csv.reader fails too, same error Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: bad argument type for built-in operation ud = vars(unix)
csv.writer(fp, **ud) Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: '_csv.Dialect' object has no attribute '__module__' del ud['__module__']
del ud['__doc__']
cw = csv.writer(fp, **ud) #if **ud works, why not 'unix'?
Third issue was mentioned above: csv.reader chokes on quotechar, even if
ignored.
cw.writerow('1 : 3'.split())
cw.writerow('1 " 3'.split())
del cw
fp.flush()
fp.close()
fp = file('csvtest', 'rb')
fp.read() # Looks good '1:\\::3\n1:":3\n' fp.seek(0)
cr = csv.reader(fp, **ud)
cr.next() ['1', ':', '3'] cr.next() Traceback (most recent call last):
File "<stdin>", line 1, in ?
_csv.Error: newline inside string

My guess is that it's trying to grab a string delimited by ", and hits the
newline before getting a matching ":
import StringIO
fp = StringIO.StringIO()
cw = csv.writer(fp, **ud)
cr = csv.reader(fp, **ud)
cw.writerow('1 " 3 " 5'.split())
fp.buflist ['1:":3:":5\n'] cr.next() ['1', ':3:', '5']

This should be ['1', '"', '3', '"', '5'].

One might just set quotechar=None for this dialect, but this raises:
cr = csv.reader(fp, quotechar=None) Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: bad argument type for built-in operation


This is contrary to PEP 305's specs:

quotechar specifies a one-character string to use as the quoting character.
It defaults to '"'. Setting this to None has the same effect as setting
quoting to csv.QUOTE_NONE.

So it seems that it is impossible for csv.reader to parse dialects which
don't use quoting.

--
Francis Avila
Jul 18 '05 #1
0 2077

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
10
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting...
22
by: Martin MOKREJ© | last post by:
Hi, I'm looking for some easy way to do something like include in c or PHP. Imagine I would like to have: cat somefile.py a = 222 b = 111 c = 9
1
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
27
by: fctk | last post by:
hello, i have some questions. 1) do constant expressions include string literals? for example, is "hello, world" a constant expression? 2) int i = 0; is the equal sign the assignment...
3
by: Scewbedew | last post by:
I have a C# service where I have included a ProjectInstaller class to handle the installation. I experiance a number of oddities, and I don't know if I'm doing anything wrong, or if the Installer...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
3
by: P Chase | last post by:
I have some resources in my C# Visual Studio 2005 project that are involved with self-test. Ideally, they shouldn't be in a released product. These resources are embedded by virtue of having...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.