如何在python中使用正则表达式从csv文件获取数据

时间:2018-10-25 20:48:47

标签: regex python-3.x csv

我正在尝试从包含许多减号的列中过滤csv文件。

我在网站上找到了源代码,该源代码可用于小型列表,但不适用于csv文件中的数据。

这是我拥有的数据的示例。

691
609
627
211
-226
921
829
1
972
173
181
-66
-96
573

这是我正在使用的代码

import pandas as pd
from pandas import DataFrame
import numpy as np
import re

import csv
from re import findall

ful = pd.read_csv(r'/home/aziz/Desktop/testminplus.csv')
ful1 = ful[0:]

#full = ['1', '-3']
full = ful1

regex = re.compile(r'(-\d*)')
# use only one of the following lines, whichever you prefer
#filtered = filter(lambda i: not regex.search(i), full)
filtered = [i for i in full if not regex.search(i)]
print(filtered)

结果如下:

[' ', ' ', ' ', ' ', '8', '2', '3', '\n', '0', ' ', ' ', ' ', ' ', '6', '0', '9', '\n', '1', ' ', ' ', ' ', ' ', '6', '2', '7', '\n', '2', ' ', ' ', ' ', ' ', '2', '1', '1', '\n', '3', ' ', ' ', ' ', '2', '2', '6', '\n', '4', ' ', ' ', ' ', ' ', '9', '2', '1', '\n', '5', ' ', ' ', ' ', ' ', '8', '2', '9', '\n', '6', ' ', ' ', ' ', ' ', ' ', ' ', '1', '\n', '7', ' ', ' ', ' ', ' ', '9', '7', '2', '\n', '8', ' ', ' ', ' ', ' ', '1', '7', '3', '\n', '9', ' ', ' ', ' ', ' ', '1', '8', '1', '\n', '1', '0', ' ', ' ', ' ', '6', '6', '\n', '1', '1', ' ', ' ', ' ', '9', '6', '\n', '1', '2', ' ', ' ', ' ', '5', '7', '3', '\n', '1', '3', ' ', ' ', ' ', '8', '9', '5', '\n', '1', '4', ' ', ' ', ' ', '1', '1', '8', '\n', '1', '5', ' ', ' ', ' ', ' ', '7', '\n', '1', '6', ' ', ' ', '6', '9', '8', '\n', '1', '7', ' ', ' ', ' ', '3', '5', '1', '\n', '1', '8', ' ', ' ', ' ', '9', '3', '3', '\n', '1', '9', ' ', ' ', ' ', '9', '3', '2', '\n', '2', '0', ' ', ' ', ' ', '7', '3', '2', '\n', '2', '1', ' ', ' ', '6', '6', '0', '\n', '2', '2', ' ', ' ', '4', '6', '5', '\n', '2', '3', ' ', ' ', ' ', '3', '4', '5', '\n', '2', '4', ' ', ' ', ' ', ' ', '1', '8', '\n', '2', '5', ' ', ' ', ' ', '1', '2', '0', '\n', '2', '6', ' ', ' ', '2', '7', '0', '\n', '2', '7', ' ', ' ', '2', '3', '3', '\n', '2', '8', ' ', ' ', '1', '5', '2', '\n', '2', '9', ' ', ' ', ' ', '1', '8', '6', '\n', '3', '0', ' ', ' ', '3', '9', '6', '\n', '3', '1', ' ', ' ', '5', '3', '5', '\n', '3', '2', ' ', ' ', ' ', '3', '5', '9', '\n', '3', '3', ' ', ' ', ' ', ' ', '1', '\n', '3', '4', ' ', ' ', '5', '3', '3', '\n', '3', '5', ' ', ' ', ' ', '8', '1', '2', '\n', '3', '6', ' ', ' ', ' ', '5', '4', '6']

所需的输出如下所示:

123
213
2
5

有什么办法解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

如果您刚得到的文件每行只有一个数字(而不是实际的CSV文件,但其中包含多个字段,这似乎不是您的情况),则可以执行以下操作:

with open('/home/aziz/Desktop/testminplus.csv') as fin:
    # generator to yield each line as an integer
    data = (int(line) for line in fin)
    # list-comp to only include positive numbers...
    positive = [n for n in data if n >= 0]

答案 1 :(得分:1)

熊猫解决方案在这里可能是一个过大的杀手,但是效果很好

import pandas as pd
# read file
df = pd.read_csv("/home/aziz/Desktop/testminplus.csv", 
                 header=None, 
                 converters={0: int}) # spits an error if non-numbers are present
# filter positives
df = df[df[0]>=0]
# write back 
df.to_csv("/home/aziz/Desktop/positives_only.csv", 
          header=False, 
          index=False)