I'm trying to parse CSV file in Python and so far it's returning a list of individual characters, but apparently I can't concatenate the list of characters into a string. When I try to do so, my logger doesn't log anything at all. Here is the code:
def action_import(self):
f = self.import_file.decode('base64')
reader = csv.reader(f)
new_reader = []
for row in reader:
new_reader += row
_logger.debug("NEW READER")
_logger.debug(new_reader) #returns a list of individual characters
string = "I'm a string"
for char in new_reader:
_logger.debug("HERE IS A CHAR")
_logger.debug(char) #returns an individual char with no quotation marks
string += str(char)
_logger.debug("THE STRING")
_logger.debug(string) #returns nothing
The output from _logger.debug(new_reader)
with the list of individual characters looks like this: ['\\xef', '\\xbb', '\\xbf', 'P', 'r', 'o', 'd', 'u', 'c', 't', '', '', 'Q', 't', 'y', '', '', 'P', 'r', 'i', 'c', 'e', '', '', 'P', 'r', 'i', 'c', 'e', ' ', 'P', 'e', 'r', ' ', 'U', 'n', 'i', 't', '', '', 'P', 'u', 'b', 'l', 'i', 'c', ' ', 'P', 'r', 'i', 'c', 'e', 'A', 'T', 'O', 'R', 'V', 'A', 'S', 'T', 'A', 'T', 'I', 'N', ' ', '2', '0', ' ', 'M', 'G', ' ', 'T', 'A', 'B', 'L', 'E', 'T', '', '', '3', '0', '', '', '1', '0', '', '', '0', '.', '3', '3', '3', '3', '3', '3', '3', '3', '3', '', '', '0', '.', '3', '3', '3', '3', '3', '3', '3', '3', '3']
I've also tried doing ''.join(new_reader)
but the same thing happens where the logger doesn't even log the result of it.
What am I doing wrong? Thank you!
str.decode()
it treated self.import_file
as a string in your case.
csv.reader()
is taking csvfile
as parameter, csvfile
is an object that supports iterator protocol and returns a string each time its next() method is called.
You accidently pass a string as the csvfile
to csv.reader()
, for example:
>>> f = csv.reader("abc")
>>> for row in f:
... print row
...
['a']
['b']
['c']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.