按准备好的行号分割文件

时间:2019-06-28 10:29:33

标签: python pandas csv

我有一个文件,我需要按特定的行索引号将其拆分为较小的文件。

我准备了索引-行标题在其中的行:

listOfHeadersIndex = []

with open('Poland.csv', 'r') as csvfile:
    for i, line in enumerate(csvfile):
        if '[Header]' in line:
            print(i)
            listOfHeadersIndex.append(i)

并且:

listOFHeadersIndex is for example = 0,33,46,74.

现在我需要将其拆分。第一个文件应包含0-32行,第二个文件应包含33-45,依此类推。我发现仅建议如何用常数行拆分。如何以简单的方式在python中完成它?

3 个答案:

答案 0 :(得分:1)

这是您要的吗?

with open('Poland.csv', 'r') as csvfile:
    raw_data = csvfile.readlines()

file1=raw_data[0:33]
file2=raw_data[33:45]

答案 1 :(得分:1)

listOfHeadersIndex = []
lines = []

with open('Poland.csv', 'r') as csvfile:
    for i, line in enumerate(csvfile):
        lines.append(line)
        if '[Header]' in line:
            print(i)
            listOfHeadersIndex.append(i)

for i,a in enumerate(listOfHeadersIndex):
    first_index = a
    last_index = listOfHeadersIndex[i+1]-1
    str_ = ''
    for j,line in enumerate(lines):
       if j >= first_index and j <= last_index:
           if str_:
              str_+= '\n'.join(line)
           else:
              str_+= line
    f = open(f"file{i}.txt", "w")
    f.write(str)
    f.close()

答案 2 :(得分:0)

您可以直接拆分文件。

例如:

newFile = False
c = 1
with open('Poland.csv', 'r') as csvfile:       #Open SRC file
    for line in csvfile:              #Iterate Each line
        if '[Header]' in line:        #Check for header
            if newFile:               #Check if file is open
                newFile.close()       #Close file.
            newFile = open("{}.txt".format(c), "w")   #Create new file for write. 
            newFile.write(line)                       #Write Header. 
            c += 1
        else:
            newFile.write(line)                       #Write Content.