首页 > 解决方案 > 分别存储不同行的csv文件

问题描述

有没有办法分别处理不同行的csv文件?我想要的是得到一个包含第一行的字符串,一个包含第二行的字符串 s2 和最后一个字符串 s3,包含从第 3 行到第len(file)-1th 行的数据

我的代码如下

with open("f1.csv") as f: 
    for line in f:  
    #s1 = 1st line
    #s2 = 2nd line
    #s3= from 3rd line no the len(file-1)
    print(line)

标签: python

解决方案


使用f.readlines()

with open("f1.csv") as f: 
   lines=f.readlines()
s1=lines[0]
s2=lines[1]
s3=lines[2:-1]  #get 3rd to last line

推荐阅读