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文件时,“文本”中的逗号被拾取并将其拆分,我尝试使用''围绕它但又一次我无法弄明白
任何帮助将不胜感激
答案 0 :(得分:1)
您可以通过将u
传递给'unicode-escape'
方法来编码unicodes,从而删除unicode.encode()
。你可以使用列表理解:
>>> 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
答案 1 :(得分:0)
" u"表示一个unicode字符串。您正在打印一个包含unicode字符串的python数组,以便在控制台上放置什么。确定你想要的东西,例如', '.join(stuff))
看起来不错。
要导出为CSV,我无法看到您的CSV代码,但我强烈建议您使用Python的csv模块来处理CSV输入和输出,并选择分隔符适合您的数据。