2012-04-18 39 views
0

这是我的CSV文件处理的第二天,我 CSV文件:提取最后一个号码在CSV中的每一行文件

-Rιalisι面值温子仁---沃斯时刻prιfιrιs! (破坏者内部)1
- 源代码 - 新闻与评论! 2
ALED - 点菜2
ALED - Bistrot餐厅6

我想在年底提取的数量并将其存储在另一个这样的文件:

hindex 
1 
2 
2 
6 

数量甚至可以两位数..

+0

你的意思是“csv”,如“逗号分隔值”? – jogojapan 2012-04-18 13:26:48

+0

什么是您的操作系统?你有什么工具可以尝试呢?发布前你有没有想过这个? – Marc 2012-04-18 13:27:03

回答

3

如果您的内容在文件中说tst.csv,你可以这样做

>>> with open("tst.csv") as fin, open("tst.out","w")as fout: 
    for line in fin: 
     fout.write(line.rpartition(" ")[-1]) 
0

这是伪代码:

foreach line 
    split the line words by space and get the last index. 
1

根据定义,CSV格式是逗号分隔的,因此我们使用split(',')infp是你输入的文件句柄(假设你的数据文件的名称是“data.csv”),outfp输出:

with open('data.csv') as infp, open('data.out', 'w') as outfp: 
    for line in infp: 
     outfp.write(line.split(',')[-1]) 

编辑:不能承受的问题的标题,显然是文件本身是不采用CSV格式的。因此,此解决方案必须使用split(' ')

相关问题