Ich habe ein Programm, das am Ende eine "Übereinstimmung" druckt Ich wollte die Daten in diesem "Spiel" zu einer CSV-Datei speichern, wie kann ich das tun? Ich habe einige Code geschrieben hat, diese Variable zu speichern, aber es schreibt nicht alles Hier mein Code ist:So speichern Sie Daten aus Python in eine CSV-Datei
import shlex
import subprocess
import os
import platform
from bs4 import BeautifulSoup
import re
import csv
import pickle
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'
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*)\s*(\S*)", text)
print(match)
with open ('score.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows('%s' %match)
Der Teil, wo ich versuchte, es in eine CSV-Datei zu speichern, ist die letzten drei Zeilen Code . Hier ist ein Ausdruck des "Match" -Formats: https://gyazo.com/930f9dad12109bc50825c91b51fb31f3
Bitte lesen Sie [fragen] und möglicherweise bieten eine [MCVE], wie im aktuellen Zustand die Antwort weder gibt eine genaue Beschreibung des von Ihnen erwarteten Ergebnisses (Format, ...) und zeigt nicht Ihren eigenen Aufwand, um das Problem zu lösen. –
Sind die letzten 3 Zeilen hier genauso eingerückt wie Ihre Datei? Wenn ja, dann liegt das Problem wohl darin. – Guillaume
Ja, sind sie, sollte ich sie tab? – fsgdfgsd