首页 > 代码库 > Python中中文输出显示以及列表初始化坑坑
Python中中文输出显示以及列表初始化坑坑
通过网查得出,使用json中的dumps方式可以解决问题。一般在需要输出得上一步使用,这样可以便于其他错误得产生
import json
# result = json.dumps(b, encoding=‘UTF-8‘, ensure_ascii=False)
# list1 = ["陈","湖北"]
# result = json.dumps(list1, encoding=‘UTF-8‘, ensure_ascii=False)
# print result
这里在初始化列表时有个坑,参考:http://www.07net01.com/2016/03/1344777.html
# CityCluster = [[],[],[],[]]
# CityCluster = [[]]*4 #坑 这里的输出最终达不到想要的上面的效果,但在输出空列表是会产生迷糊,
#以为是上面的结果,但输出非空列表时和上述的效果不一样。
CityCluster = [list() for i in xrange (k)] #这样才正确
# -*- coding:utf-8 -*-
import numpy as np
from sklearn.cluster import KMeans
import json
def loadData(filePath):
fr = open(filePath,‘r+‘)
lines = fr.readlines()
retData = http://www.mamicode.com/[]
retCityName = []
for line in lines:
items = line.strip().split(",")
retCityName.append(items[0])
retData.append([float(items[i]) for i in range(1,len(items))])
for i in range(1,len(items)):
return retData,retCityName
if __name__==‘__main__‘:
data,cityName = loadData(‘city.txt‘)
# cityName = json.dumps(cityName,encoding="GBK",ensure_ascii=False)
# print (cityName)
k = 3
km = KMeans(n_clusters=k)
label = km.fit_predict(data)
expenses = np.sum(km.cluster_centers_,axis=1)
print(expenses)
print label
# CityCluster = [[],[],[],[]]
# CityCluster = [[]]*4 #坑
CityCluster = [list() for i in xrange (k)] #这样才正确
for i in range(len(cityName)):
CityCluster[label[i]].append(cityName[i])
for i in range(len(CityCluster)):
print("Expenses:%.2f" % expenses[i])
CityCluster[i] = json.dumps(CityCluster[i],encoding="GBK",ensure_ascii=False) #为了能够顺利输出中文
print(CityCluster[i])
Python中中文输出显示以及列表初始化坑坑