0

I got a csv file like this

"5478",a,56.40,-0.40 ,55.50,57.50,55.30,56.74,"862,971","48,962,460","695",56.40,56.60,"127,474,332",56.40,60.30,52.50
"5480",b,21.90,-0.25 ,21.80,22.00,21.80,21.87,"1,598,041","34,950,597","590",21.90,21.95,"199,097,830",21.90,23.40,20.40
"70462P",c,0.01,-0.01 ,0.01,0.01,0.01,0.01,"99,000","990","1",0.01,0.06,"5,000,000",0.01,0.31,0.01
"70465P",d, ---,--- ,---,---,---,0.02,"0","0","0",0.01,0.03,"20,000,000",0.02,0.32,0.01
"8935",bt,5.02,-0.02 ,4.95,5.19,4.92,5.05,"949,102","4,791,070","290",5.02,5.07,"201,902,107",5.02,5.37,4.67
1333,tnd,21.40,-0.60 ,22.00,22.20,21.20,21.52,"1,519,292","32,692,804","631",21.40,21.50,"102,525,625",21.40,22.85,19.95

I'd like to check the first column, if it is over 4 digits, then remove the line, so for example, the 2nd and the 3rd row will be removed. How do I do this? thanks a lot

ps2 This is stock information downloaded from stock center, but I found the format is changed recently, the format before is as the last line, the first column is without quotation "", is it possible to filter the two formats? or I should deal with the 2 cases?

4

5 回答 5

3

这是一个sed解决方案:

sed -e '/^"[0-9]\{5\}/d' in-file > out-file

-i您还可以使用以下选项进行就地替换:

sed -i -e '/^"[0-9]\{5\}/d' file
于 2013-06-09T08:50:23.243 回答
1

Not sure, what language u'd like, because u marked awk and sed in tags, but You could simply use grep:

egrep '^\"[0-9]{1,4}\"' file.txt
于 2013-06-09T08:58:28.573 回答
1

awk

awk -F, '$1~ /^\"[0-9][0-9]?[0-9]?[0-9]?\"$/' file

GNU sed

sed '/^\"[0-9]\{1,4\}\"/!d' file
于 2013-06-09T10:37:21.807 回答
0
import csv, tempfile, shutil

with open('data.csv', 'rb') as fin, \
     tempfile.NamedTemporaryFile(delete=False) as fout:
    w = csv.writer(fout)
    for row in csv.reader(fin):
        if len(row[0]) <= 4:
            w.writerow(row)

shutil.move(fout.name, 'data.csv')
于 2013-06-09T08:44:33.497 回答
0

你可以做这样的事情。我假设lines是一个迭代器,每个循环从 CSV 文件中给出一行。

for line in lines:
    if len(line.split(',')[0]) <= 4: #consider this line
        print line #process this line
    else:
        pass
于 2013-06-09T08:45:11.710 回答