Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从python中的csv文件中获取上面两个字段的内容_Python_Csv - Fatal编程技术网

从python中的csv文件中获取上面两个字段的内容

从python中的csv文件中获取上面两个字段的内容,python,csv,Python,Csv,在一个文件中,我有行。在行中,我有字段。在这里,我正在搜索只包含数字的字段。我需要打印当前字段上方两个字段的字段内容。比如说, with open("file1.csv", "rb") as f: reader = csv.reader(f, delimiter=',') for row in reader: for field in row: if field.isdigit(): print two_fi

在一个文件中,我有行。在行中,我有字段。在这里,我正在搜索只包含数字的字段。我需要打印当前字段上方两个字段的字段内容。比如说,

with open("file1.csv", "rb") as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        for field in row:
            if field.isdigit():
                print two_fields_above_current_field

任何帮助都将不胜感激。

像这样的方法应该可以奏效

with open("file1.csv", "r") as f:
data = []
for i in f:
  data.append(i.split(','))
#or use csvreader..


for y in range(0,len(data)):
  for x in range(0,len(data[y])):
    if(y>=2 and data[y][x].isdigit()):
      print(data[y-2][x])

您是指您以前读过两行的字段[i]吗?你可以把你的行读入一个数组,然后当你被击中时再回头看。当然,这是重复的