简体   繁体   English

在Python中仅读取csv文件的更新部分

[英]Read only the updated part of a csv file in Python

I have a csv file in which I append new results every 10 seconds. 我有一个csv文件,其中每10秒附加一次新结果。 Every so (specified from a user) I read this csv and make a JSON file. 如此(从用户指定),我阅读了此csv并制作了JSON文件。 I don't want to convert the whole csv into a JSON every time but only the last part that has been updated. 我不想每次都将整个csv转换为JSON,而只想更新最后一部分。 So, I imagine that I will keep the last line every time, then I will start reading the file from this line and then to convert it to a JSON format. 因此,我想我每次都会保留最后一行,然后开始从这一行读取文件,然后将其转换为JSON格式。

myJson = {}
try:
    with open('myFile.csv', 'r') as f: #Read the csv file with read privileges.
            for line in f:
                lst = re.split(r' +', line.rstrip('\t')) #Columns are seperated with tabs.
                if len(lst) == 3: #There are three columns in the file:
                    n = lst[0].strip() #Names 
                    v = lst[1].strip() #Values 
                    p = lst[2].strip() #Percentages 
                    try:
                        myJson[n].append(v) #Add values to the according keys.
                    except KeyError:
                            myJson[n] = [v] #Handle potential KeyErrors.
except IOError:
    print "csv file has not been created yet."

The simplest way is to delete the csv file each time but it would be much more useful for me to keep it and just create new JSON files. 最简单的方法是每次都删除csv文件,但对我而言,保留它并仅创建新的JSON文件将更加有用。

I would suggest to use another protocol to get your data shuffled from your producer to your consumer, for example a socket or a pipe. 我建议使用另一种协议,例如套接字或管道,将数据从生产者转移到消费者。 The approach pointed to by Lutz also works of course, but it relies on using a tool provided by the OS (tail). Lutz指出的方法当然也可以使用,但是它依赖于使用OS提供的工具(尾部)。

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

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