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

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

使用python从csv文件中提取部分数据,python,python-3.x,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 whil

问题

从文件中提取数据块

说明

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                    
将open('messy.csv','rt')作为文件输入,将open('nice.csv','w')作为文件输出:
对于文件中的行:
如果行中有“字符串”:
开始=0

而start这对你有用吗

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  
将open('messy.csv','rt')作为文件输入,将open('nice.csv','w')作为文件输出:
复制标志=False
开始=0
对于文件中的行:
如果行中有“字符串”:
复制标志=True
如果复制_标志并开始<20:
fileout.write(行)
开始+=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  
将open('messy.csv','rt')作为文件输入,将open('nice.csv','w')作为文件输出:
复制标志=False
开始=0
对于文件中的行:
如果行中有“字符串”:
复制标志=True
如果复制_标志并开始<20:
fileout.write(行)
开始+=1
我没有得到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
将open('messy.csv','rt')作为文件输入,将open('nice.csv','w')作为文件输出:
copy=False#初始化为False,因为我们仍然不知道密钥是否存在
开始=0
first=True#确保我们只比较第一行
对于文件中的行:
如果第行中的'string'和first==True:
复制=真
第一个=错误
如果copy==True且start<20:#现在我们知道键存在,我们复制20行
fileout.write(行)
开始+=1
我没有得到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
将open('messy.csv','rt')作为文件输入,将open('nice.csv','w')作为文件输出:
copy=False#初始化为False,因为我们仍然不知道密钥是否存在
开始=0
first=True#确保我们只比较第一行
对于文件中的行:
如果第行中的'string'和first==True:
复制=真
第一个=错误
如果copy==True且start<20:#现在我们知道键存在,我们复制20行
fileout.write(行)
开始+=1

while
更改为
if
while
更改为
if
但字符串仅位于第一行,它还能工作吗?我想我应该澄清一下,对不起,是的,会的。你试过了吗?哦,是的,很好。但我不能理解这一点:在内嵌的第二行中,第二个条件为false。为什么它还能工作?当满足“字符串”条件时,复制标志被设置为True。(第6行),如果标志为True且开始时间小于20,则行将写入输出,但字符串仅在第一行,它还能工作吗?我想我应该澄清一下,对不起,是的,会的。你试过了吗?哦,是的,很好。但我不能理解这一点:在内嵌的第二行中,第二个条件为false。为什么它还能工作?当满足“字符串”条件时,复制标志被设置为True。(第6行),如果标志为True,且开始值小于20,则第2行将写入输出