使用python从csv文件中提取部分数据

时间:2019-05-16 18:12:44

标签: python python-3.x

问题

从文件中提取数据块。

说明

Python读取文件。如果找到特定的字符串,它将开始将所有下一行复制到输出文件中(包括第一行),直到“关闭”为止。 密钥字符串仅在第一行。

尝试

with open ('messy.csv', 'rt') as filein, open('nice.csv', 'w') as fileout:  
    for line in filein:
      if 'string' in line:  
        start=0
        while start<20:
          fileout.write(line)
          start+=1                    

当然,此代码的问题是复制的行未实现(仅重复第一行)。您能帮我解决吗? (实际上并不需要完整的解决方案)

2 个答案:

答案 0 :(得分:1)

这对您有用吗?

with open ('messy.csv', 'rt') as filein, open('nice.csv', 'w') as fileout:  
    copy_flag = False
    start=0
    for line in filein:
        if 'string' in line:  
            copy_flag = True
        if copy_flag and start < 20:
            fileout.write(line)
            start+=1  

答案 1 :(得分:0)

我没有得到 start 变量,所以它将复制20行(包括第一行),对吧?

问题在于您仅复制第一行,因为它是唯一包含键并接受if条件的行。试试这个:

with open ('messy.csv', 'rt') as filein, open('nice.csv', 'w') as fileout:
    copy = False  #Initializes as false because we still don't know if the key exists
    start = 0
    first = True  #Make sure that we only compare the first line

    for line in filein:
      if 'string' in line and first == True:  
        copy = True
      first = False
      if copy == True and start < 20:   #Now we know that the key exists, we copy 20 lines
        fileout.write(line)
        start += 1