在Python中读取CSV文件会产生单个字符的列表
我试图用Python解析CSV文件,到目前为止,它返回一个单独字符的列表,但显然我无法将字符列表连接到字符串中。当我尝试这样做时,我的记录器根本不记录任何东西。代码如下:在Python中读取CSV文件会产生单个字符的列表,python,csv,logging,Python,Csv,Logging,我试图用Python解析CSV文件,到目前为止,它返回一个单独字符的列表,但显然我无法将字符列表连接到字符串中。当我尝试这样做时,我的记录器根本不记录任何东西。代码如下: def action_import(self): f = self.import_file.decode('base64') reader = csv.reader(f) new_reader = [] for row in reader: new_reader += row
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
带有单个字符列表的\u logger.debug(新的\u阅读器)
的输出如下所示:
['\xef','\xbb','\xbf','P','r','o','d','u','c','t','y','y','P','r','i','e','e','P','r','c','e','e','P','P','e','r','r','r','r','u','n','t','P','u','b','l','i','c','P','P','r','c','e','A','t','t','o','r','r','r','V','A','t','t','t','t','t','t','t','t','t','t','i','t','M','S“,”,“,“T”,“A”,“B”,“L”,“E”,“T”,“3”,“0”,“1”,“0”,“0”,“0”,“0”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3”,“3
我也尝试过做'.join(new_reader)
,但同样的事情发生在记录器甚至没有记录结果的地方
我做错了什么?谢谢!
str.decode()
在您的案例中,它将self.import\u文件
作为字符串处理
csv.reader()
将csvfile
作为参数,csvfile
是一个支持迭代器协议的对象,每次调用其next()方法时都返回一个字符串
您意外地将字符串作为csvfile
传递给csv.reader()
,例如:
>>> f = csv.reader("abc")
>>> for row in f:
... print row
...
['a']
['b']
['c']
str.decode()
在您的案例中,它将self.import\u文件
作为字符串处理
csv.reader()
将csvfile
作为参数,csvfile
是一个支持迭代器协议的对象,每次调用其next()方法时都返回一个字符串
您意外地将字符串作为csvfile
传递给csv.reader()
,例如:
>>> f = csv.reader("abc")
>>> for row in f:
... print row
...
['a']
['b']
['c']
看起来像是self.import\u file
是一个字符串,而不是一个文件处理程序。它是如何初始化的?如果是这种情况,csv.reader(f.split(“\n”)
可能会help@Maratself.import\u file.decode('base64').split(“\r”)
成功了--谢谢你的想法!它看起来像是一个字符串,而不是一个文件处理程序。它是如何初始化的?如果是这样,csv.reader(f.split(“\n”)
可能help@Maratself.import\u file.decode('base64').split(“\r”)
奏效了——谢谢你的想法!事实证明,我甚至不必使用csv.reader(),因为我处理的是字符串。相反,我使用了产品价格表向导导入
开始。谢谢你的帮助!事实证明我甚至不必使用csv.reader()因为我处理的是字符串,所以我使用了product\u pricelist\u wizard\u import
开始。谢谢您的帮助!