Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.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_Python 3.4 - Fatal编程技术网

Python 在csv文件中启动一个特定行

Python 在csv文件中启动一个特定行,python,csv,python-3.4,Python,Csv,Python 3.4,我有一个大文件,我想从for循环中结束的那一行开始 def ip_to_land(whatIP, startline): with open("cord.csv") as csvfile: # 100640 total lines readCSV = csv.reader(csvfile, delimiter = ",") for row in readCSV: IP_LOW = IPAddress(row[0]) IP_HIG

我有一个大文件,我想从for循环中结束的那一行开始

def ip_to_land(whatIP, startline):

    with open("cord.csv") as csvfile:
    # 100640 total lines
    readCSV = csv.reader(csvfile, delimiter = ",")

    for row in readCSV:
        IP_LOW = IPAddress(row[0])
        IP_HIGH = IPAddress(row[1])
        Land = row[2]

        start_line += 1

        if IPAddress(whatIP)>=IP_LOW and IPAddress(whatIP)<=IP_HIGH:
            return Land, row[3], row[4], start_line
            readCSV.seek(start_line)
            break
def ip_to_land(whatIP,startine):
打开(“cord.csv”)作为csvfile:
#共100640条线路
readCSV=csv.reader(csvfile,分隔符=“,”)
对于readCSV中的行:
IP_LOW=IP地址(第[0]行)
IP_高=IP地址(第[1]行)
土地=行[2]
起始线+=1

如果IPAddress(whatIP)>=IP_LOW,并且IPAddress(whatIP)假设
readCSV
是一个打开阅读的文件,而不是
csv.reader
(不可查找),则查找的是字节偏移量,而不是偏移量

因此,使用例如

start_line = readCSV.tell()
以便以后能够回到同一地点


文件的
tell
方法返回要读取的偏移量(以字节为单位)。正如网站上的文档所说,如果您在Windows上,您可能需要在模式
rb
(读取二进制)下打开文件,以确保
tell
seek
按需工作(否则可能会出现妨碍正常操作的行尾转换)。

这可能会有所帮助。我假设readCSV是文件所有行的列表

    readCSV= open('filename.CSV').readlines() #list of all the lines in file

    for row in range(len(readCSV)):

        if something:
            #your code
            break

    print readCSV[row+1]  

如果退出循环时
row
是33,则
print
调用将打印行列表中的下一行。

我已将实际代码添加到顶部。还有可能吗?由于缩进问题,很难说——请编辑Q,缩进四个空格,而不是制表符。到目前为止,
seek
break
的确切位置以及原因是相当神秘的。一般来说,这是可能的,但是字节偏移量(仍然是可以查找的位置)和当前行号(不适用于查找),如果出于不同目的需要后者,则必须单独维护。