简体   繁体   English

使用python读取CSV文件

[英]Reading CSV file with python

filename = 'NTS.csv'
mycsv = open(filename, 'r')
mycsv.seek(0, os.SEEK_END)

while 1:
   time.sleep(1)
   where = mycsv.tell()
   line = mycsv.readline()

if not line:
    mycsv.seek(where)
else:
    arr_line = line.split(',')
    var3 = arr_line[3]

    print (var3)

I have this Paython code which is reading the values from a csv file every time there is a new line printed in the csv from external program. 我有此Paython代码,每次从外部程序的csv中打印新行时,该代码都将从csv文件中读取值。 My problem is that the csv file is periodically completely rewriten and then python stops reading the new lines. 我的问题是,csv文件会定期被完全重写,然后python停止读取新行。 My guess is that python is stuck on some line number and the new update can put maybe 50 more or less lines. 我的猜测是python停留在某些行号上,新的更新可能会增加或减少50行。 So for example python is now waiting a new line at line 70 and the new line has come at line 95. I think the solution is to let mycsv.seek(0, os.SEEK_END) been updated but not sure how to do that. 因此,例如python现在正在第70行等待新行,而新行已经在95行。我认为解决方案是让mycsv.seek(0,os.SEEK_END)得到更新,但不确定如何执行。

You can do a simpler way of reading lines in your program. 您可以采用一种更简单的方法来读取程序中的行。 Instead of trying to use seek in order to get what you need, try using readlines on the file object mycsv . 与其尝试使用seek来获取所需内容, mycsv尝试在文件对象mycsv上使用readlines

You can do the following: 您可以执行以下操作:

mycsv = open('NTS.csv', 'r')
csv_lines = mycsv.readlines()

for line in csv_lines:
    arr_line = line.split(',')
    var3 = arr_line[3]
    print(var3)

What you want to do is difficult to accomplish without rewinding the file every time to make sure that you are truly on the last line. 如果不每次都快退文件以确保您真正处在最后一行,则很难完成您想做的事情。 If you know approximately how many characters there are on each line, then there is a shortcut you could take using mycsv.seek(-end_buf, os.SEEK_END), as outlined in this answer . 如果您知道每行大约有多少个字符,那么可以使用mycsv.seek(-end_buf,os.SEEK_END)采取快捷方式,如本答案所述 So your code could work somehow like this: 因此,您的代码可以这样工作:

avg_len = 50  # use an appropriate number here
end_buf = 3 * avg_len / 2

filename = 'NTS.csv'
mycsv = open(filename, 'r')
mycsv.seek(-end_buf, os.SEEK_END)
last = mycsv.readlines()[-1]

while 1:

    time.sleep(1)
    mycsv.seek(-end_buf, os.SEEK_END)
    line = mycsv.readlines()[-1]

    if not line == last:

        arr_line = line.split(',')
        var3 = arr_line[3]

        print (var3)

Here, in each iteration of the while loop, you seek to a position close to the end of the file, just far back enough that you know for sure the last line will be contained in what remains. 在这里,在while循环的每次迭代中,您都寻找到靠近文件末尾的位置,恰好足够远以至于您确定最后一行将包含在剩余的行中。 Then you read in all the remaining lines (this will probably include a partial amount of the second or third to last lines) and check if the last line of these is different to what you had before. 然后,您读完所有剩余的行(这可能包括第二行或第三行到最后一行的一部分),并检查这些行的最后一行是否不同于您以前的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM