简体   繁体   English

提取CSV文件每行中的最后一个数字

[英]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.

相关问题 在 csv 文件中查找字符串时提取一行 - Extract a row while looking for a string in a csv file 如何每隔一行读取CSV文件 - How to read a CSV file every other row 从附加 CSV 文件中删除最后一行 - Removing last row from appending CSV file 如何更改.csv文件中一行的最后一个值 - How to change the last value of a row in .csv file 如何为CSV文件中的每一列提取两个字符之间的子字符串,并将这些值复制到Python中的新列中? - How can I extract a substring between two characters for every row of a column in a CSV file and copy those values into a new column in Python? Python:遍历CSV的每一行,计数每一行中的令牌,创建一个具有原始CSV每行令牌数量的新CSV - Python: Iterate over every row of a CSV, count the tokens in every row, create a new CSV with the number of tokens for each row of the original CSV 查找特定的vocab并提取一行csv并在新的csv文件中进行转换 - find specific vocab and extract row of csv and transform in new csv file 使用 python 从 csv 文件中获取最后一列的最后一行 - Get last row in last column from a csv file using python 计算文件每一行中的逗号数 - Counting number of commas in every row of a file 如何在 csv 文件(Python 3)中查找行并从行中提取数据 - How to find a row and extract data from row in csv file (Python 3)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM