使用Python从CSV文件中提取数据行

我有一个专有档案格式的大文件。 解压这个压缩文件给出了一个没有扩展名的文件,但里面的数据是用逗号分隔的。 添加一个.csv扩展名或简单地用Excel打开该文件将工作。

我有大约375-400这些文件,我试图提取关键字“A点”和另一个关键字“点B”之间的一大块行(约1.2万行+13500)。

我发现这个网站上的一些代码,我认为是正确的提取数据,但我得到一个错误:

AttributeError: 'list' object has no attribute 'rows'当试图保存文件时, AttributeError: 'list' object has no attribute 'rows' 。 有人可以帮我把这些数据保存成CSV文件吗?

 import re import csv import time print(time.ctime()) file = open('C:/Users/User/Desktop/File with No Extension That\'s Very Similar to CSV', 'r') data = file.read() x = re.findall(r'Point A(.*?)Point B', data,re.DOTALL) name = "C:/Users/User/Desktop/testoutput.csv" with open(name, 'w', newline='') as file2: savefile = csv.writer(file2) for i in x.rows: savefile.writerow([cell.value for cell in i]) print(time.ctime()) 

在此先感谢,任何帮助将不胜感激。

以下应该很好地工作。 如上所述,你的正则expression式的使用几乎是正确的。 通过将find的文本转换为StringIO对象并将其传递给CSV阅读器,仍然可以使用Python CSV库进行CSV处理:

 import re import csv import time import StringIO print(time.ctime()) input_name = "C:/Users/User/Desktop/File with No Extension That's Very Similar to CSV" output_name = "C:/Users/User/Desktop/testoutput.csv" with open(input_name, 'r') as f_input, open(output_name, 'wb') as f_output: # Read whole file in all_input = f_input.read() # Extract interesting lines ab_input = re.findall(r'Point A(.*?)Point B', all_input, re.DOTALL)[0] # Convert into a file object and parse using the CSV reader fab_input = StringIO.StringIO(ab_input) csv_input = csv.reader(fab_input) csv_output = csv.writer(f_output) # Iterate a row at a time from the input for input_row in csv_input: # Skip any empty rows if input_row: # Write row at a time to the output csv_output.writerow(input_row) print(time.ctime()) 

你还没有给我们从你的CSV文件的例子,所以如果有问题,你可能需要configurationCSV的方言来更好地处理它。

testing使用Python 2.7

这里有两个问题:第一个与正则expression式有关,另一个与列表语法有关。

  1. 得到你想要的

    您使用正则expression式的方式将返回给您一个具有单个值的列表(所有行都转换为唯一的string)。

    也许有更好的方法做到这一点,但我现在要走这样的事情:

     with open('bla', 'r') as input: data = input.read() x = re.findall(r'Point A(.*?)Point B', data, re.DOTALL)[0] x = x.splitlines(False)[1:] 

    这不是很好,但会返回一个列表,其中包含这两个点之间的所有值。

  2. 使用列表

    列表内没有rows属性。 你只需要遍历它:

     for i in x: do what you have to do 

    看,我不熟悉的csv库,但它看起来你将不得不执行一些操作的i值之前,将其添加到图书馆。

恕我直言,我会避免使用CSV格式,因为它是一种“区域依赖”,所以它可能无法正常工作,取决于最终用户可能在操作系统上的设置。