Extra Newline vor ----- Endzertifikat ----- Bei der Bearbeitung von CSV in PythonPython

Python-Programme
Anonymous
 Extra Newline vor ----- Endzertifikat ----- Bei der Bearbeitung von CSV in Python

Post by Anonymous »

Ich veriere mich in Python, um Daten aus einer anderen Datei zu extrahieren und zu formatieren. Beim Schreiben der Ausgabe erhalte ich jedoch eine zusätzliche neue Zeile vor ----- Endzertifikat- ---. Ich möchte, dass die Ausgabe zuvor eine neue Linie druckt

Code: Select all

 -----END CERTIFICATE-----
, aber mein aktueller Code fügt eine extra leere Zeile hinzu, so ist es so, als würde es ihnen entkommen, und ich verstehe nicht, warum
ich versucht habe.strip () im Zertifikat und im öffentlichen Schlüssel So entfernen Sie alle führenden oder nachfolgenden Whitespace, aber die zusätzliche Neue Leitung wurde immer noch angezeigt. > Aber das Ende ist nicht < /p>
Ein Rat, wie man dies korrigiert? < /p>

Code: Select all

import csv
import os

# Define input files and output file
input_files = ['11296205_124_2025.csv', '11296208_113_2025.csv']
output_file = 'inventory_upload.csv'

# Prepare the output data list
converted_data = []

# Process each input file
for input_file in input_files:
if not os.path.exists(input_file):
print(f"Warning: {input_file} not found, skipping.")
continue

with open(input_file, mode='r', encoding='utf-8') as infile:
reader = csv.DictReader(infile)
for row in reader:
# Get the certificate and key values
tpm_certificate = row['TPM Certificate'].strip()
gek_public_key = row['GEK Public Key'].strip()

# Remove existing markers and normalize newlines
tpm_certificate = tpm_certificate.replace("-----BEGIN CERTIFICATE-----", "").replace("-----END CERTIFICATE-----", "")
gek_public_key = gek_public_key.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "")

# Ensure there are no extra empty lines
tpm_certificate = "\n".join([line.strip() for line in tpm_certificate.splitlines() if line.strip()])
gek_public_key = "\n".join([line.strip() for line in gek_public_key.splitlines() if line.strip()])

# Correctly format the certificate without extra newlines
tpm_certificate = f"-----BEGIN CERTIFICATE-----\n{tpm_certificate}\n-----END CERTIFICATE-----\n'"
gek_public_key = f"-----BEGIN PUBLIC KEY-----\n{gek_public_key}\n-----END PUBLIC KEY-----\n'"

# Append processed data
converted_data.append([
row['Hardware Model'].strip(),
row['Hardware Serial Number'].strip(),
tpm_certificate,
gek_public_key,
""  # Empty UUID field
])

# Write the combined output file
with open(output_file, mode='w', encoding='utf-8', newline='') as outfile:
writer = csv.writer(outfile, quoting=csv.QUOTE_MINIMAL)
# Write the header
writer.writerow(["Model", "Serial", "EK Certificate", "GEK Public Key", "UUID"])
# Write the processed data
writer.writerows(converted_data)

print(f"File converted and saved as {output_file}")

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post