Python 如何读取/打印特定的列和行
我试图通过一个csv文件(像电子表格一样的行和列)读取数据,并让它在给定2个参数的情况下找到数据的特定点 def总管(a、b): 其中a是列,b是行,因此如果我键入“a,2”,它将给出列(a)的名称和第2行中的数字。我不知道该怎么做 这就是我试过的Python 如何读取/打印特定的列和行,python,csv,python-3.x,Python,Csv,Python 3.x,我试图通过一个csv文件(像电子表格一样的行和列)读取数据,并让它在给定2个参数的情况下找到数据的特定点 def总管(a、b): 其中a是列,b是行,因此如果我键入“a,2”,它将给出列(a)的名称和第2行中的数字。我不知道该怎么做 这就是我试过的 def data(a, b): file = open("file.csv") csv_file = csv.reader(file) for line in csv_file: array = line.
def data(a, b):
file = open("file.csv")
csv_file = csv.reader(file)
for line in csv_file:
array = line.split(",")
first_item = array[0]
a = len(array)
csvfile.seek(0)
reader = csv.reader(csv_file, delimiter=" ")
for row in reader:
b = list(row[a] for a in included_cols)
print(content)
您可以将文件读入二维数组,然后使用a、b索引到数组中
def data(a, b):
array = []
with open("file.csv") as file:
for line in file.readlines():
array.append(line.split(","))
print array[a][b]
使用with open(“file.csv”)作为文件,将在您退出with代码块时关闭文件您需要显示您已尝试的内容,您是否能够打开并解析csv文件?打印(数组[a][b])类型错误:列表索引必须是整数或切片,而不是str我一直遇到此错误,我认为输入a和b的值时是b/c,第一个值是列(a、b、c等)的字符串,而第二个值是int