[英]extract last number in every row of CSV file
This is my second day of csv file handling, my CSV file: 这是我处理CSV文件的第二天,即CSV文件:
-Rιalisι par James Wan--- Vos moments prιfιrιs ! -Rαalisα占詹姆斯万--- Vos时刻prαr¹s! (Spoilers inside) 1 (里面的扰流板)1
-Source Code- News et Critique ! -源代码-新闻和评论! 2 2
ALED - A la carte 2 ALED-点菜2
ALED - Bistrot 6 ALED-Bistrot 6
I would like to extract the number at the end and store it in another file like this: 我想在末尾提取数字并将其存储在另一个文件中,如下所示:
hindex
1
2
2
6
The number could even be two digit.. 该数字甚至可以是两位数。
If your content is in a file say tst.csv
you can do something like 如果您的内容在文件中,请说tst.csv
您可以执行以下操作
>>> with open("tst.csv") as fin, open("tst.out","w" )as fout:
for line in fin:
fout.write(line.rpartition(" ")[-1])
By definition the csv format is comma separated, therefore we use split(',')
. 根据定义,csv格式是逗号分隔的,因此我们使用split(',')
。 infp
is your input file handle (assuming the name of your data file is 'data.csv'), outfp
for output: 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])
EDIT: not withstanding the title of the question, apparently the file itself is not in CSV format. 编辑:不承受问题的标题,显然文件本身不是 CSV格式。 Therefore this solution would have to use split(' ')
. 因此,该解决方案必须使用split(' ')
。
This is pseudo code: 这是伪代码:
foreach line
split the line words by space and get the last index.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.