2017-05-05 4 views
0

Mein Programm tut alles, was ich will, aber speichert nicht die endgültigen Daten in die CSV-Datei, habe ich einen Druck vor es um zu sehen, ob die Daten richtig waren und es ist, es schreibt gerade nicht in die CSV-Datei, ich benutze 'a', weil ich nicht will, dass es neu schreibt, was bereits geschrieben ist, aber es gibt immer noch einen Fehler zurück.Probleme beim Speichern von etwas in einer CSV-Datei

hier ist der Teil des Codes:

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) 

Hier ist der Fehler:

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 

Hier ist der gesamte Programmcode bei Bedarf

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) 

EDIT

Es funktioniert, hier ist der Code arbeiten

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) 

Antwort

0

Am Ende gibt es ein Problem mit Ihrem Code

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

Siehe initialisieren Sie den Schriftsteller, aber dann benutzen Sie es nicht.

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

Jetzt wird promping es einen anderen Fehler Traceback (jüngste Aufforderung zuletzt): Datei "C: \ PROJECT \ pdfs \ final.py", Linie 103, in writer.writerow (csvline) UnicodeEncodeError: 'ascii' Codec kann das Zeichen u '\ xa0' nicht codieren in Position 0: Ordnungszahl nicht im Bereich (128) – fsgdfgsd

+0

Welche Art von Fehler? – VKolev

+0

Sie sollten sehen, wie Sie Ihre Daten in Unicode 'csvline.encode ('utf-8')' 'verschlüsseln oder die Datei auf utf-8 codieren' mit open ('.... csv', 'w', encoding = 'utf-8') als f' – VKolev

0

hier:

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

Du Instanziierung eine csv.writer, aber ihn nicht benutzen. Dies sollte lauten:

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

Nun gibt es durchaus ein paar andere Probleme mit Ihrem Code, die erste der ‚csvline als Text manuell erstellen wird dann csv.writer Verwendung zu speichern Datei. csv.writer.write() erwartet eine Liste von Zeilen (Tupel) und kümmert sich um das richtige Entweichen, was maskiert werden muss, Einfügen der richtigen Trennzeichen usw. Es hat auch eine writerow() Methode, die ein einzelnes Tupel nimmt und so die gesamte Liste im Speicher FWIW zu erstellen.

+0

Aber als ich versuchte, zu tun, wie Sie es schrieb immer noch einen Fehler über die Codierung, versuchte ich zu ende-8 codieren und es ist ein weiterer Fehler – fsgdfgsd

+0

@ Samuel prompting, das ein anderes Problem ist. Sie müssen (offensichtlich) die Textteile Ihrer Zeilen zuerst in die gewünschte Kodierung kodieren. Wenn Sie Probleme damit haben, informieren Sie sich zuerst über Unicode und Encodings (ein heute benötigtes Wissen) und schreiben Sie eine weitere Frage (mit allen relevanten Details), falls Sie noch Fehler haben. –

Verwandte Themen