excel - Extracting Rows of Data from a CSV-like File Using Python -
i have large file proprietary archive format. unzipping archive gives file has no extension, data inside comma-delimited. adding .csv extension or opening file excel work.
i have 375-400 of these files, , i'm trying extract chunk of rows (about 13,500 out of 1.2m+ rows) between keyword "point a" , keyword "point b".
i found code on site think extracting data correctly, i'm getting error:
attributeerror: 'list' object has no attribute 'rows' when trying save out file. can me data save csv?
import re import csv import time print(time.ctime()) file = open('c:/users/user/desktop/file no extension that\'s similar csv', 'r') data = file.read() x = re.findall(r'point a(.*?)point b', data,re.dotall) name = "c:/users/user/desktop/testoutput.csv" open(name, 'w', newline='') file2: savefile = csv.writer(file2) in x.rows: savefile.writerow([cell.value cell in i]) print(time.ctime()) thanks in advance, appreciated.
the following should work nicely. mentioned, regex usage correct. possible still use python csv library csv processing converting found text stringio object , passing csv reader:
import re import csv import time import stringio print(time.ctime()) input_name = "c:/users/user/desktop/file no extension that's similar csv" output_name = "c:/users/user/desktop/testoutput.csv" open(input_name, 'r') f_input, open(output_name, 'wb') 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 file object , parse using csv reader fab_input = stringio.stringio(ab_input) csv_input = csv.reader(fab_input) csv_output = csv.writer(f_output) # iterate row @ time input input_row in csv_input: # skip empty rows if input_row: # write row @ time output csv_output.writerow(input_row) print(time.ctime()) you have not given example csv file, if there problems, might need configure csv 'dialect' process better.
tested using python 2.7
Comments
Post a Comment