gpt4 book ai didi

Python:搜索 csv 并返回整行

转载 作者:太空狗 更新时间:2023-10-29 21:39:51 24 4
gpt4 key购买 nike

我找不到更好的地方来问我的问题。我正在学习 Python 并尝试创建如下脚本。

1) 应该能够搜索 csv 文件。

2) 如果找到匹配则返回整行。

我的 csv:

Product,Scan,Width,Height,Capacity
LR,2999,76,100,17.5
RT,2938,37,87,13.4

如果我搜索 2938 作为示例,则整行返回如下:

Product: RT
Scan: 2938
Width: 37
Height: 87
Capacity: 13,4

到目前为止我有:

csvFile = getComponent().filePath
pos = csvFile.rfind('Desktop\\')
csvFile = csvFile[:pos] + 'programm\\products.csv'

myfile = open(csvFile)
myfile.seek(0)
for line in myfile.split('\n'):
data = line.split(',')
print data
if data[2] == agv_O_Cal.value and data[3] == agv_O_Mod.value:
print 'found: value = %s' %agv_O_Cal.value, agv_O_Mod.value
Product = data[5]
Scan = data[6]
Width = data[7]
Height = data[9]
Capacity = data[10]
print , Product, Scan, Width, Height, Capacity

解决方案不起作用。

最佳答案

#!/usr/bin/python

import csv
import sys

#input number you want to search
number = raw_input('Enter number to find\n')

#read csv, and split on "," the line
csv_file = csv.reader(open('test.csv', "r"), delimiter=",")


#loop through the csv list
for row in csv_file:
#if current rows 2nd value is equal to input, print that row
if number == row[1]:
print (row)

关于Python:搜索 csv 并返回整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26082360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com