Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #68698
| Date | 2014-03-21 14:59 +0000 |
|---|---|
| From | Tim Golden <mail@timgolden.me.uk> |
| Subject | Re: csv read _csv.Error: line contains NULL byte |
| References | <22aeefa3-cf82-457c-ab85-6f0366ff7b4e@googlegroups.com> <mailman.8354.1395409181.18130.python-list@python.org> <fefcec40-3bd9-4a94-9ae8-4f214fce2302@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.8361.1395414009.18130.python-list@python.org> (permalink) |
On 21/03/2014 14:46, chip9munk@gmail.com wrote:
> I am sorry I do not understand how to get to each row in this way.
>
> Please could you explain also this:
> If I define this function,
> how do I change my for loop to get each row?
Does this help?
<code>
#!python3
import csv
def unfussy_reader(csv_reader):
while True:
try:
yield next(csv_reader)
except csv.Error:
# log the problem or whatever
print("Problem with some row")
continue
if __name__ == '__main__':
#
# Generate malformed csv file for
# demonstration purposes
#
with open("temp.csv", "w") as fout:
fout.write("abc,def\nghi\x00,klm\n123,456")
#
# Open the malformed file for reading, fire up a
# conventional CSV reader over it, wrap that reader
# in our "unfussy" generator and enumerate over that
# generator.
#
with open("temp.csv") as fin:
reader = unfussy_reader(csv.reader(fin))
for n, row in enumerate(reader):
print(n, "=>", row)
</code>
TJG
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
csv read _csv.Error: line contains NULL byte chip9munk@gmail.com - 2014-03-21 06:29 -0700
Re: csv read _csv.Error: line contains NULL byte Tim Golden <mail@timgolden.me.uk> - 2014-03-21 13:39 +0000
Re: csv read _csv.Error: line contains NULL byte chip9munk@gmail.com - 2014-03-21 07:46 -0700
Re: csv read _csv.Error: line contains NULL byte chip9munk@gmail.com - 2014-03-21 07:59 -0700
Re: csv read _csv.Error: line contains NULL byte Tim Golden <mail@timgolden.me.uk> - 2014-03-21 14:59 +0000
Re: csv read _csv.Error: line contains NULL byte Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-21 15:15 +0000
csiph-web