python - 将每行中的多个 csv 元素读取到单独的列表中
问题描述
我有一个 CSV,我想将每行的第 6 个元素读入一个列表,并将每行的第 7 个元素读入另一个列表。下面是我的代码:
import csv
import math
#open csv file and create fie reader
with open('weightheightdata.csv', 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
#skip first 2 lines (header lines)
next(reader)
next(reader)
#create list of e values using the 9th column of each row
actualWeight = []
predictedWeight = []
for row in reader:
actualWeight.append(float(row[6]))
predictedWeight.append(float(row[7]))
我收到错误:TabError: inconsistent use of tabs and spaces in indentation
在线:predictedWeight.append(float(row[7]))
。
解决方案
您的代码运行良好。我自己测试过。只需检查您的缩进就行了。确保您使用了与整个文件相同的制表符或 4 个空格。这实际上就是错误的全部。Python 对缩进非常严格。
推荐阅读
- php - PHP中的PdfParser问题
- python - Django模型-如何通过相关模型使用过滤器
- html - Xpath在tbody内按位置4阶td查找文本并单击同一行中的编辑
- c++ - C++ 将原始 std::bytes 复制到 std::vector
- javascript - 如何在javascript中的一个选择下拉列表中显示所有父值和子值
- python - 带有rest框架的django在线用户
- oracle - Oracle Application Express 的问题
- google-cloud-platform - ESP32 到 GCP IOT Core 连接失败
- css - 输入滑块(v-slider)死区上的标签
- c# - 当父母移动平台时,角色移动缓慢