如何阅读特定编号CSV文件的行数?

问题描述 投票:0回答:1

我正在尝试使用以下代码读取*。csv文件]的前10行] >>(the actual file has 79 rows and 12 columns),但是每次它返回的文件都是79行,但我只想要前10行。请帮助我要去哪里.. !!谢谢!!

#!/bin/bash
OLD=$IFS
IFS=","
i=1
while [ $i -lt 10 ]
    do
        j=1
        while [ $j -le 12 ]
            do
                if [ $j -lt 12 ]
                    then
                        read -r -d $IFS f1
                        echo -n "$f1," >> out.csv
                else
                    read -r -d $IFS f1
                    echo -n "$f1" >> out.csv
                fi
                j=`expr $j + 1`
        done
        i=`expr $i + 1`
done < $1
IFS=$OLD

我正在尝试使用以下代码读取* .csv文件的前10行(实际文件具有79行和12列),但是每次它返回给我一个包含所有79行的文件时,我只希望第一个...

bash shell windows-subsystem-for-linux kali-linux
1个回答
0
投票
with open("a*.csv", "rb") as infile:
r = csv.reader(infile):
for i in range(8): # count from 0 to 7
    next(r)     # and discard the rows
row = next(r)   # "row" contains row number 9 now
© www.soinside.com 2019 - 2024. All rights reserved.