Skip to main content
 首页 » 编程设计

Unicode 和 csv 问题中的 Python 数组打印

2025年05月04日8leader

dict1 是一个字典,对应的数组有 4 个样本元素,如下所示:

{u'OlpyplEJ_c_hFxyand_Wxw': [u'Inchin Bamboo Garden', u'Paradise Valley', 33.575816, -111.926234],  
u'_qvxFHGbnbrAPeWBVifJEQ': [u"Lenny's Sub Shop", u'Charlotte', 35.334993, -80.8129717],  
u's5yzZITWU_RcJzWOgjFecw': [u"Sergio's Italian Gardens", u'Las Vegas', 36.100414, -115.1265829]} 

我正在使用 business_id 作为上述字典的键来打印数据

print "%s,%s" % (dict1[jd['business_id']], re.sub('\n|\r', '', jd['text'])) 

示例输出是:

[u"P&G's Pamela's Diner", u'Pittsburgh', 40.451723, -79.932833], The food here is over the top excessively greasy. So greasy that it made me sick to my stomach before I was done eating my meal. My husband and I split the chocolate chip pancakes and a ham and cheese omelette with potatoes and toast on the side. Not only was everything in a pool of grease, but it seemed to be margarine...not even real butter. I will never eat here again. I am gagging even thinking about this meal let aloe eating it again. 

我有两个问题,第一个,如何从数组中的 2 个输出字符串中删除 unicode 标记 u',我试过 str() 但它不起作用

第二个,当我导出为 .csv 文件时,“文本”中的逗号被拾取并将其拆分,我尝试在它周围使用“”,但我还是无法弄清楚

任何帮助将不胜感激

请您参考如下方法:

您可以通过将 'unicode-escape' 传递给 unicode.encode() 方法对 unicode 进行编码来删除 u。你可以使用列表理解:

>>> l=[u"P&G's Pamela's Diner", u'Pittsburgh', 40.451723, -79.932833] 
>>> [i.encode('unicode-escape') if isinstance(i,unicode) else i for i in l] 
["P&G's Pamela's Diner", 'Pittsburgh', 40.451723, -79.932833] 

关于你的第二个问题,因为 python 默认情况下会假定逗号作为分隔符。为了摆脱它,你可以为它定义一个服装定界符。

例如:

import csv 
with open('file_name.csv', 'wb') as csvfile: 
    spamwriter = csv.writer(csvfile, delimiter=' ') 
    #do stuff