简体   繁体   English

从csv文件中提取数字

[英]Extracting numbers from a csv file

I wrote a small python script in order to extract numbers from a csv file I wrote on the shell. 我写了一个小的python脚本,以便从我在shell上编写的csv文件中提取数字。

from csv import reader

def ext(fileName):
    l= []
    with open(fileName, delimiter = '\n' ) as inp:
        for row in inp:
            l += [row]
    print(l)

With my test file, I get: 有了我的测试文件,我得到:

['1\n', '2\n', '5\n', '3\n', '6\n']

How can I make clear that I am extracting numbers and not strings? 我怎样才能说清楚我是在提取数字而不是字符串呢? (I want to store these numbers in a list) (我想将这些数字存储在一个列表中)

instead of l += [row] , use l.append(int(row)) . 而不是l += [row] ,使用l.append(int(row)) This should work 这应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM