读取导入csv csv报错iterable expected, not float

示例代码
import pandas as pd
import re
import csv

data = pd.read_csv('nuojia.csv', encoding='utf-8')
# print(data)
data = data.values

# 把第二列的数值数据提取出来,存入num.csv中
with open('num.csv', 'a+', newline='') as csvfile:
writer = csv.writer(csvfile)
data1 = data[:, 1]
print(data1)
# print(len(data[:, 1]))
for i in range(len(data1)):
# print(data1[i])
num = re.findall('[0-9]+', data1[i])
# print(num)
# writer.writerow(num)

注意:1,csv导入大部分是字典格式
2,列表的话一般要求,列表里面的是字符串格式
3,如果是一列数据不是字符串,写入csv的话会报错iterable expected, not float
那就把数据存入列表中,导入整个列表,再把数据在excel中转置(选中数据,复制,选择性粘贴,转置),代码如下:
comment_score = []
with open('comment_score.csv', 'a+', newline='') as csvfile:
writer = csv.writer(csvfile)
for i in range(1,len(columns)):
comment = columns[i]
try:
s = SnowNLP(columns[i])
print(s.sentiments)
comment_score.append(s.sentiments)
except ZeroDivisionError as e:
print('ZeroDivisionError')

print(comment_score)
writer.writerow(comment_score)
4,参考链接如下:https://blog.csdn.net/taotiezhengfeng/article/details/75876717

读取csv
with open('nuojia.csv','rt', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
columns = [row[6] for row in reader] # 读取第七列
不想用dataframe格式只想要数据的话就用这种方法

存入数据不知道怎么存的话就可以用dataframe
kmodel = KMeans(n_clusters=k, n_jobs=1)
kmodel.fit(data)

print(kmodel.cluster_centers_) # 查看聚类中心
center = DataFrame(kmodel.cluster_centers_)
center.to_csv('center.csv')
print(kmodel.labels_) # 查看个样本对应的类别
label = DataFrame(kmodel.labels_)
label.to_csv('label.csv')

上一篇:【POJ】1556 The Doors(计算几何基础+spfa)


下一篇:HTTP Live Streaming直播(iOS直播)技术分析与实现