I'm processing CSV files in Python to extract and format data from another file. However, when writing the output, I get an extra newline before -----END CERTIFICATE--
---. I want the output to have print one newline before -----END CERTIFICATE-----
, but my current code adds an extra blank line. It's like it's escaping it, and I don't understand why
i tried using.strip() on the certificate and public key to remove any leading or trailing whitespace, but the extra newline still appeared.
The begin certificate part is correct with -----BEGIN CERTIFICATE----- \n
but the ending isnt
Any advice on how to correct this?
"Hardware Model","Hardware Serial Number","Software Vesrion","TPM Certificate","GEK Public Key"
"Dell-XR5610","22RR664","2409.202409271735","-----BEGIN CERTIFICATE-----\nMIICjzCCAhSgAwIBAgIKc6+Fk3yadBQW+TAKBggqhkjOPQQDAzBdMSUwIwYDVQQD\nDBxOUENUeHh4IEVDQzM4NCBMZWFmQ0EgMDIyMTExMScwJQYDVQQKDB5OdXZvdG9u\nIFRlY2hub2xvZ3kgQ29ycG9yYXRpb24xC3sVvijggEXMIIBEzAOBgNVHQ6BAf8EBAMCAwgwEAYDVR0l\nBAkwBwYFZ4EFCAEwDAYDVR0TAQH/BAIwADAfBgNVHSMEGDAWgBQyvcyCpzfMWoBa\ny7sZglpi1PuZ9TBqBggrBgEFBQcBAQReMFwwWgYIKwY3Lm51dm90b24uY29tL3NlY3VyaXR5L05UQy1UUE0tRUstQ2VydC9OUENUeHh4\nRUNDMzg0TGVhZkNBMDIyMTExLmNlcjBUBgNVHREBAf8ESjBIpEYwRDEWMBQGBWeB\nBQIBDAtpZDo0RTU0N4MwMDESMBAGBWeBBQICDAdOUENUNzV4MRYwFAYFZ4EFAgMM\nC2lkOjAwMDcwMDAyMAoGCCqGSM49BAMDA2kAMGYCMQDs1PpbyoRm2skDiXAPUKHZ\nYV596T9DfRFrKGzOTVVEDB2UanEqmKxz2M8JfVT5tswCMQDzVjNtmJDn2WM+n4lO\nVjsyICkwSSEDcmjcKu1/Ciyx3c5HbU/VbG7iUrPLvLDQ5vQ=\n-----END CERTIFICATE-----\n","-----BEGIN PUBLIC KEY-----\nMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE/j4t0qBsEaJ1y7bNTANk4IurNcxwmFab\nL8M+OWVeSnjlQxWsKt8FuIRlS8O0eHGfBFFW1P1SoH+mct6/0z/zJG6fJU7yiePr\nlCjvu2C0FdOR5X4cvJN6f43MJ2bnbq2N\n-----END PUBLIC KEY-----\n"
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}")
I'm processing CSV files in Python to extract and format data from another file. However, when writing the output, I get an extra newline before -----END CERTIFICATE--
---. I want the output to have print one newline before -----END CERTIFICATE-----
, but my current code adds an extra blank line. It's like it's escaping it, and I don't understand why
i tried using.strip() on the certificate and public key to remove any leading or trailing whitespace, but the extra newline still appeared.
The begin certificate part is correct with -----BEGIN CERTIFICATE----- \n
but the ending isnt
Any advice on how to correct this?
"Hardware Model","Hardware Serial Number","Software Vesrion","TPM Certificate","GEK Public Key"
"Dell-XR5610","22RR664","2409.202409271735","-----BEGIN CERTIFICATE-----\nMIICjzCCAhSgAwIBAgIKc6+Fk3yadBQW+TAKBggqhkjOPQQDAzBdMSUwIwYDVQQD\nDBxOUENUeHh4IEVDQzM4NCBMZWFmQ0EgMDIyMTExMScwJQYDVQQKDB5OdXZvdG9u\nIFRlY2hub2xvZ3kgQ29ycG9yYXRpb24xC3sVvijggEXMIIBEzAOBgNVHQ6BAf8EBAMCAwgwEAYDVR0l\nBAkwBwYFZ4EFCAEwDAYDVR0TAQH/BAIwADAfBgNVHSMEGDAWgBQyvcyCpzfMWoBa\ny7sZglpi1PuZ9TBqBggrBgEFBQcBAQReMFwwWgYIKwY3Lm51dm90b24uY29tL3NlY3VyaXR5L05UQy1UUE0tRUstQ2VydC9OUENUeHh4\nRUNDMzg0TGVhZkNBMDIyMTExLmNlcjBUBgNVHREBAf8ESjBIpEYwRDEWMBQGBWeB\nBQIBDAtpZDo0RTU0N4MwMDESMBAGBWeBBQICDAdOUENUNzV4MRYwFAYFZ4EFAgMM\nC2lkOjAwMDcwMDAyMAoGCCqGSM49BAMDA2kAMGYCMQDs1PpbyoRm2skDiXAPUKHZ\nYV596T9DfRFrKGzOTVVEDB2UanEqmKxz2M8JfVT5tswCMQDzVjNtmJDn2WM+n4lO\nVjsyICkwSSEDcmjcKu1/Ciyx3c5HbU/VbG7iUrPLvLDQ5vQ=\n-----END CERTIFICATE-----\n","-----BEGIN PUBLIC KEY-----\nMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE/j4t0qBsEaJ1y7bNTANk4IurNcxwmFab\nL8M+OWVeSnjlQxWsKt8FuIRlS8O0eHGfBFFW1P1SoH+mct6/0z/zJG6fJU7yiePr\nlCjvu2C0FdOR5X4cvJN6f43MJ2bnbq2N\n-----END PUBLIC KEY-----\n"
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}")
Share
Improve this question
edited Feb 16 at 19:33
Zach Young
11.2k4 gold badges36 silver badges56 bronze badges
asked Feb 14 at 5:29
Mbuyi TuambilanganaMbuyi Tuambilangana
113 bronze badges
12
|
Show 7 more comments
1 Answer
Reset to default 0I took your input CSV, converted escaped newlines (\\n
) to literal newlines (\n
) and reduced the long strings to:
"Hardware Model","Hardware Serial Number","Software Vesrion","TPM Certificate","GEK Public Key"
"Dell-XR5610","22RR664","2409.202409271735","-----BEGIN CERTIFICATE-----
MIICjzCCAhSgAwIBAgIKc6+.../VbG7iUrPLvLDQ5vQ=
-----END CERTIFICATE-----
","-----BEGIN PUBLIC KEY-----
MHYwEAYHKoZIzj0CAQYFK4E...JN6f43MJ2bnbq2N
-----END PUBLIC KEY-----
"
I ran your script as-is (swapping in my input and output file names), and got:
Model,Serial,EK Certificate,GEK Public Key,UUID
Dell-XR5610,22RR664,"-----BEGIN CERTIFICATE-----
MIICjzCCAhSgAwIBAgIKc6+.../VbG7iUrPLvLDQ5vQ=
-----END CERTIFICATE-----
'","-----BEGIN PUBLIC KEY-----
MHYwEAYHKoZIzj0CAQYFK4E...JN6f43MJ2bnbq2N
-----END PUBLIC KEY-----
'",
So... I don't see the problem... do you? I do see the single quote your code introduces, but I don't think that's related, so I'm ignoring it for now.
I'm running this on a Mac. What OS are you running this on?
-----BEGIN CERTIFICATE-----
lines because those are part of PEM formatted keys and certificates. I would expect the whole block from the first to the last-
to remain integral. – msanford Commented Feb 14 at 16:21tpm_certificate
ends with a newline, and you're adding another one in the f-string. So get rid of that newline. Or use{tmp_certificate.rstrip()}
– Barmar Commented Feb 14 at 16:36