用python迭代文本文件中的列

用python迭代文本文件中的列,python,python-2.7,Python,Python 2.7,我有一个以下格式的文本文件: 1,"20130219111529","90UP:34","0000","9999","356708","2" "-2","20130219105824","0001:11","0000","","162_005","" 出于某种目的,我想比较第1行和第2行(在本例中为1和-2)。要去掉所有引号并解析此文件,我有以下代码: if os.path.exists(FileName): with open(FileName) as File: f

我有一个以下格式的文本文件:

1,"20130219111529","90UP:34","0000","9999","356708","2"
"-2","20130219105824","0001:11","0000","","162_005",""
出于某种目的,我想比较第1行和第2行(在本例中为1和-2)。要去掉所有引号并解析此文件,我有以下代码:

if os.path.exists(FileName):
    with open(FileName) as File:
        for row in csv.reader(File, delimiter= ',', skipinitialspace= True):
            print(row)
以下是输出:

['1', '20130219111529', '90UP:34', '0000', '9999', '356708', '2']
['-2', '20130219105824', '0001:11', '0000', '', '162_005', '']
我想遍历这些列。例如,遍历“1”,然后遍历“-2”,依此类推


如何执行此操作?

只需打印行中的第一个元素:

for row in csv.reader(File, delimiter= ',', skipinitialspace= True):
            print(row[0])
编辑


只需打印行中的第一个元素:

for row in csv.reader(File, delimiter= ',', skipinitialspace= True):
            print(row[0])
编辑

使用
zip()
。它将两个iterable转换为元组的一个iterable,元素来自两个列表

l1 = ['1', '20130219111529', '90UP:34', '0000', '9999', '356708', '2']
l2 = ['-2', '20130219105824', '0001:11', '0000', '', '162_005', '']

for elem1, elem2 in zip(l1, l2):
    print("elem1 is {0} and elem2 is {1}.".format(elem1, elem2)
使用
zip()
。它将两个iterable转换为元组的一个iterable,元素来自两个列表

l1 = ['1', '20130219111529', '90UP:34', '0000', '9999', '356708', '2']
l2 = ['-2', '20130219105824', '0001:11', '0000', '', '162_005', '']

for elem1, elem2 in zip(l1, l2):
    print("elem1 is {0} and elem2 is {1}.".format(elem1, elem2)
也许是以下几点

if os.path.exists(FileName):
    with open(FileName) as File:
        lastRow = []
        # loop over the lines in the file
        for row in csv.reader(File, delimiter= ',', skipinitialspace= True):
            # saves the first row, for comparison below
            if lastRow == []:
                lastRow = row
                continue

            # loop over the columns, if all rows have the same number
            for colNum in range(len(row)):
                # compare row[colNum] and lastRow[colNum] as you wish

            # save this row, to compare with the next row in the loop
            lastRow = row
也许是以下几点

if os.path.exists(FileName):
    with open(FileName) as File:
        lastRow = []
        # loop over the lines in the file
        for row in csv.reader(File, delimiter= ',', skipinitialspace= True):
            # saves the first row, for comparison below
            if lastRow == []:
                lastRow = row
                continue

            # loop over the columns, if all rows have the same number
            for colNum in range(len(row)):
                # compare row[colNum] and lastRow[colNum] as you wish

            # save this row, to compare with the next row in the loop
            lastRow = row
如果(正如您在问题中所说,尽管我不确定您是否希望这样做)您希望迭代列,您可以执行以下操作:

if os.path.exists(file_name):
    with open(file_name) as csv_file:
        for columns in zip(*csv.reader(csv_file, delimiter=',', skipinitialspace=True)):
            print columns
('1', '-2')
('20130219111529', '20130219105824')
('90UP:34', '0001:11')
('0000', '0000')
('9999', '')
('356708', '162_005')
('2', '')
这将输出以下内容:

if os.path.exists(file_name):
    with open(file_name) as csv_file:
        for columns in zip(*csv.reader(csv_file, delimiter=',', skipinitialspace=True)):
            print columns
('1', '-2')
('20130219111529', '20130219105824')
('90UP:34', '0001:11')
('0000', '0000')
('9999', '')
('356708', '162_005')
('2', '')
如果(正如您在问题中所说,尽管我不确定您是否希望这样做)您希望迭代列,您可以执行以下操作:

if os.path.exists(file_name):
    with open(file_name) as csv_file:
        for columns in zip(*csv.reader(csv_file, delimiter=',', skipinitialspace=True)):
            print columns
('1', '-2')
('20130219111529', '20130219105824')
('90UP:34', '0001:11')
('0000', '0000')
('9999', '')
('356708', '162_005')
('2', '')
这将输出以下内容:

if os.path.exists(file_name):
    with open(file_name) as csv_file:
        for columns in zip(*csv.reader(csv_file, delimiter=',', skipinitialspace=True)):
            print columns
('1', '-2')
('20130219111529', '20130219105824')
('90UP:34', '0001:11')
('0000', '0000')
('9999', '')
('356708', '162_005')
('2', '')

我做到了。输出是1-2我很困惑,为什么在打印(行)时显示两个列表,而在执行行[0]时只显示元素?好的,csv.reader返回包含单个行的列表。因此,当您
打印行
时,您打印的是整个列表,但当您
打印行[0]
时,您打印的是列表中的第一个元素
,这在jramirez看来是有意义的。我想您的意思是打印(len(row))而不是“rows”?不,我的意思是
rows
这个
row
变量只是for循环的局部变量,因此如果在它引发异常之前使用它,它将引发异常。我做到了。输出是1-2我很困惑,为什么在打印(行)时显示两个列表,而在执行行[0]时只显示元素?好的,csv.reader返回包含单个行的列表。因此,当您
打印行
时,您打印的是整个列表,但当您
打印行[0]
时,您打印的是列表中的第一个元素
,这在jramirez看来是有意义的。我想您的意思是打印(len(row))而不是“行”?不,我的意思是
行变量仅是for循环的局部变量,因此如果在它引发异常之前使用它,我将如何访问1和-2?如何为它们编制索引?columns变量是由两个元素组成的元组,因此可以使用列[0]和列[1]。下一次循环时,这些相同的变量将为您提供下一列的数据,依此类推……如果我在for循环中输入print(列[0]),它会给我以下结果:我的意图是:如果(元素==1):#做点什么20130219111529打印在1下面。90UP:34打印在20130219111529下方,以此类推。很抱歉格式错误,请忽略我的评论。我真是太傻了,竟然把它贴出来。在这种方法中,我怎么能访问1和-2呢?如何为它们编制索引?columns变量是由两个元素组成的元组,因此可以使用列[0]和列[1]。下一次循环时,这些相同的变量将为您提供下一列的数据,依此类推……如果我在for循环中输入print(列[0]),它会给我以下结果:我的意图是:如果(元素==1):#做点什么20130219111529打印在1下面。90UP:34打印在20130219111529下方,以此类推。很抱歉格式错误,请忽略我的评论。我真是太傻了,竟然把它贴了出来。