python - 分别存储不同行的csv文件
问题描述
有没有办法分别处理不同行的csv文件?我想要的是得到一个包含第一行的字符串,一个包含第二行的字符串 s2 和最后一个字符串 s3,包含从第 3 行到第len(file)-1
th 行的数据
我的代码如下
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)
解决方案
使用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
推荐阅读
- android - 如何在 android-studio 中用 Kotlin 制作半圈进度条?
- bash - VS Shell 命令“代码”安装返回“命令不可用”
- asp.net-core - 从自定义的错误请求响应内部记录
- php - 我怎样才能在保留回复的同时移动我的评论?
- ios - 为什么 UITabbarController 太低了
- laravel - laravel 5.8 如何在 laravel 控制器中使用 if 和 else if 函数
- git - VSCode 中的 S 文件标记是什么意思?
- javascript - 尝试添加新对象时,我的 javascript 数组不断被覆盖
- python - 将 pandas 数据帧插入 SQL
- google-apps-script - 如何将“调整行大小”设置从“适合数据”更改为“指定行高,不,无论如何我都希望该行高 21 像素”?