I have the following code:
import os
import csv
listing = os.listdir('/directory/my/files/are/in')
os.chdir('/directory/my/files/are/in')
for file in listing[1:]:
r = csv.reader(open(file, 'rU'))
for row in r:
stuff = [str.split('\t', 1) for row in r]
print stuff
which returns TypeError: expected a character buffer object
. How do I fix t开发者_开发知识库his? Basically I want to open all (csv) files in a directory and then cut out certain junk from each cell (everything after and including /t.)
str.split('\t', 1)
is the equivalent of '\t'.split(1)
, which tries to split the string '\t'
by the non-string 1
, which fails. And it's not entirely clear what you expect this to do: you're iterating over r
, and then in the body of that loop you are iterating over r
again (in the list comprehension.) Did you instead mean to do something like this?
for row in r:
stuff = [item.split('\t', 1) for item in row]
print stuff
(Which still doesn't make much sense, but at least it does something.)
import os
import csv
listing = os.listdir('/directory/my/files/are/in')
os.chdir('/directory/my/files/are/in')
for file in listing[1:]:
if os.path.isdir(file):
continue
with open(file, 'rU') as csvfile:
for row in csv.reader(csvfile, delimiter='\t'):
print row
#now you can extract data from the row
This fixes several problems.
First, you need to make sure each file to try to open is actually a file, not a directory.
Second, csv.reader
can use tabs as the delimiter. This means you don't need to use split
.
Third, this version actually closes the files.
精彩评论