2017-05-05 119 views
0

我的程序完成了所有我想要的操作,但没有将最终数据保存到csv文件中,我在它之前使用了一个打印来查看数据是否正确,只是没有写入csv文件,我使用'a',因为我不希望它重写已写入的内容,但它仍然返回错误。将某些东西保存到csv文件时遇到困难

这里的部分代码:

soup = BeautifulSoup(answer) 
        for table in soup.findAll('table', {"class":"formTable"}): 
         for row in table.findAll('tr'): 
          #heading = row.find('td', {"class":"sectionHeading"}) 
          #if heading is not None: 
           #print(heading.get_text()); 
          #else: 
          label = row.find('td', {"class":"fieldLabel"}) 
          data = row.find('td', {"class":"fieldData"}) 
          if data is not None and label is not None: 
             csvline += label.get_text() + "," + data.get_text() + "," 
        print(csvline) 
        #csvline.encode('utf-8') 
        with open ('output_file_two.csv', 'a', encoding='utf-8') as f: 
         writer = csv.writer(f) 
         writer.writerow(csvline) 

这里的错误:

Traceback (most recent call last): 
    File "C:\PROJECT\pdfs\final.py", line 95, in <module> 
    with open ('output_file_two.csv', 'a', encoding='utf-8') as f: 
TypeError: 'encoding' is an invalid keyword argument for this function 

下面是需要

import shlex 
import subprocess 
import os 
import platform 
from bs4 import BeautifulSoup 
import re 
#import unicodecsv as csv 
import csv 
#import pickle 
import requests 
from robobrowser import RoboBrowser 
import codecs 

def rename_files(): 
    file_list = os.listdir(r"C:\\PROJECT\\pdfs") 
    print(file_list) 
    saved_path = os.getcwd() 
    print('Current working directory is '+saved_path) 
    os.chdir(r'C:\\PROJECT\\pdfs') 
    for file_name in file_list: 
     os.rename(file_name, file_name.translate(None, " ")) 
    os.chdir(saved_path) 
rename_files() 

def run(command): 
    if platform.system() != 'Windows': 
     args = shlex.split(command) 
    else: 
     args = command 
    s = subprocess.Popen(args, 
         stdout=subprocess.PIPE, 
         stderr=subprocess.PIPE) 
    output, errors = s.communicate() 
    return s.returncode == 0, output, errors 

# Change this to your PDF file base directory 
base_directory = 'C:\\PROJECT\\pdfs' 
if not os.path.isdir(base_directory): 
    print "%s is not a directory" % base_directory 
    exit(1) 
# Change this to your pdf2htmlEX executable location 
bin_path = 'C:\\Python27\\pdfminer-20140328\\tools\\pdf2txt.py' 
if not os.path.isfile(bin_path): 
    print "Could not find %s" % bin_path 
    exit(1) 
for dir_path, dir_name_list, file_name_list in os.walk(base_directory): 
    for file_name in file_name_list: 
     # If this is not a PDF file 
     if not file_name.endswith('.pdf'): 
      # Skip it 
      continue 
     file_path = os.path.join(dir_path, file_name) 
     # Convert your PDF to HTML here 
     args = (bin_path, file_name, file_path) 
     success, output, errors = run("python %s -o %s.html %s " %args) 
     if not success: 
      print "Could not convert %s to HTML" % file_path 
      print "%s" % errors 
htmls_path = 'C:\\PROJECT' 
with open ('score.csv', 'w') as f: 
    writer = csv.writer(f) 
    for dir_path, dir_name_list, file_name_list in os.walk(htmls_path): 
     for file_name in file_name_list: 
      if not file_name.endswith('.html'): 
       continue 
      with open(file_name) as markup: 
       soup = BeautifulSoup(markup.read()) 
       text = soup.get_text() 
       match = re.findall("PA/(\S*)", text)#To remove the names that appear, just remove the last (\S*), to add them is just add the (\S*), before it there was a \s* 
       print(match) 
       writer.writerow(match) 
       for item in match: 
        data = item.split('/') 
        case_number = data[0] 
        case_year = data[1] 
        csvline = case_number + "," 

        browser = RoboBrowser() 
        browser.open('http://www.pa.org.mt/page.aspx?n=63C70E73&CaseType=PA') 
        form = browser.get_forms()[0] # Get the first form on the page 
        form['ctl00$PageContent$ContentControl$ctl00$txtCaseNo'].value = case_number 
        form['ctl00$PageContent$ContentControl$ctl00$txtCaseYear'].value = case_year 

        browser.submit_form(form, submit=form['ctl00$PageContent$ContentControl$ctl00$btnSubmit']) 

        # Use BeautifulSoup to parse this data 
        answer = browser.response.text 
        #print(answer) 
        soup = BeautifulSoup(answer) 
        for table in soup.findAll('table', {"class":"formTable"}): 
         for row in table.findAll('tr'): 
          #heading = row.find('td', {"class":"sectionHeading"}) 
          #if heading is not None: 
           #print(heading.get_text()); 
          #else: 
          label = row.find('td', {"class":"fieldLabel"}) 
          data = row.find('td', {"class":"fieldData"}) 
          if data is not None and label is not None: 
             csvline += label.get_text() + "," + data.get_text() + "," 
        print(csvline) 
        with open ('output_file_two.csv', 'a') as f: 
         writer = csv.writer(f) 
         writer.writerow(csvline) 

编辑

的情况下,整个程序代码

它的工作,这里的工作

import shlex 
import subprocess 
import os 
import platform 
from bs4 import BeautifulSoup 
import re 
import unicodecsv as csv 
import requests 
from robobrowser import RoboBrowser 
import codecs 

def rename_files(): 
    file_list = os.listdir(r"C:\\PROJECT\\pdfs") 
    print(file_list) 
    saved_path = os.getcwd() 
    print('Current working directory is '+saved_path) 
    os.chdir(r'C:\\PROJECT\\pdfs') 
    for file_name in file_list: 
     os.rename(file_name, file_name.translate(None, " ")) 
    os.chdir(saved_path) 
rename_files() 

def run(command): 
    if platform.system() != 'Windows': 
     args = shlex.split(command) 
    else: 
     args = command 
    s = subprocess.Popen(args, 
         stdout=subprocess.PIPE, 
         stderr=subprocess.PIPE) 
    output, errors = s.communicate() 
    return s.returncode == 0, output, errors 


base_directory = 'C:\\PROJECT\\pdfs' 
if not os.path.isdir(base_directory): 
    print "%s is not a directory" % base_directory 
    exit(1) 

bin_path = 'C:\\Python27\\pdfminer-20140328\\tools\\pdf2txt.py' 
if not os.path.isfile(bin_path): 
    print "Could not find %s" % bin_path 
    exit(1) 
for dir_path, dir_name_list, file_name_list in os.walk(base_directory): 
    for file_name in file_name_list: 

     if not file_name.endswith('.pdf'): 

      continue 
     file_path = os.path.join(dir_path, file_name) 

     args = (bin_path, file_name, file_path) 
     success, output, errors = run("python %s -o %s.html %s " %args) 
     if not success: 
      print "Could not convert %s to HTML" % file_path 
      print "%s" % errors 
htmls_path = 'C:\\PROJECT' 
with open ('score.csv', 'w') as f: 
    writer = csv.writer(f) 
    for dir_path, dir_name_list, file_name_list in os.walk(htmls_path): 
     for file_name in file_name_list: 
      if not file_name.endswith('.html'): 
       continue 
      with open(file_name) as markup: 
       soup = BeautifulSoup(markup.read()) 
       text = soup.get_text() 
       match = re.findall("PA/(\S*)", text) 
       print(match) 
       writer.writerow(match) 
       for item in match: 
        data = item.split('/') 
        case_number = data[0] 
        case_year = data[1] 
        csvline = case_number + "," 

        browser = RoboBrowser() 
        browser.open('http://www.pa.org.mt/page.aspx?n=63C70E73&CaseType=PA') 
        form = browser.get_forms()[0] 
        form['ctl00$PageContent$ContentControl$ctl00$txtCaseNo'].value = case_number 
        form['ctl00$PageContent$ContentControl$ctl00$txtCaseYear'].value = case_year 

        browser.submit_form(form, submit=form['ctl00$PageContent$ContentControl$ctl00$btnSubmit']) 


        answer = browser.response.text 
        soup = BeautifulSoup(answer) 
        for table in soup.findAll('table', {"class":"formTable"}): 
         for row in table.findAll('tr'): 
          label = row.find('td', {"class":"fieldLabel"}) 
          data = row.find('td', {"class":"fieldData"}) 
          if data is not None and label is not None: 
           csvline += label.get_text() + "," + data.get_text() + "," 
           print(csvline) 
           my_file = codecs.open('final_output.csv', 'a', 'utf-8') 
           my_file.write(csvline) 

回答

0

在最后的代码中有你的代码

writer = csv.writer(f) 
csv.writer(csvline) # here is the problem 

看你初始化作家的一个问题,但你不使用它。

writer = csv.writer(f) 
writer.writerow(csvline) 
+0

现在它promping另一个错误 回溯(最近通话最后一个): 文件 “C:\ PROJECT \ PDF文档\ final.py”,线路103,在 writer.writerow(csvline) UnicodeEncodeError: 'ascii'编解码器不能编码字符u'\ xa0'在 位置0:序号不在范围内(128) – fsgdfgsd

+0

什么样的错误? – VKolev

+0

你应该看看如何将数据编码为Unicode'csvline.encode('utf-8')'或者将文件设置为utf-8编码',打开('.... csv','w',encoding = 'utf-8')作为f' – VKolev

0

这里:

with open ('output_file_two.csv', 'a') as f: 
    writer = csv.writer(f) 
    csv.writer (csvline) 

您instanciating一个csv.writer,但不使用它。这应该阅读:

with open ('output_file_two.csv', 'a') as f: 
    writer = csv.writer(f) 
    writer.write(csvline) 

现在有不少其他问题与您的代码,第一个是手动创建“csvline作为然后用csv.writer存储到文件的文本。 csv.writer.write()需要行(元组)的列表,并负责正确地转义需要转义的内容,插入正确的分隔符等。它还有一个采用单个元组的方法,因此避免在内存FWIW中构建整个列表。

+0

但是,当我试图做你写的它仍然提示有关编码的错误,我试图使用编码uft-8,并提出了另一个错误 – fsgdfgsd

+0

@Samuel这是另一个问题。您(显然)必须先将行的文本部分编码为所需的编码。如果您遇到问题,请首先了解unicode和编码(现在需要的知识),然后发布另一个问题(包含所有相关详细信息),如果您仍有一些错误。 –