最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Extra newline before -----END CERTIFICATE----- when processing CSV in Python - Stack Overflow

programmeradmin3浏览0评论

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
  • Please provide a minimal reproducible example, which in particular means that it contains the required input data, too. Make sure that and the code are minimal though. For example, I don't think you need more than two columns to demonstrate this. – Ulrich Eckhardt Commented Feb 14 at 11:11
  • Can you provide a sample input? I don't understand the purpose in manipulating the -----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:21
  • If you're getting an extra newline it's presumably because tpm_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
  • @msanford The sample input is a device cert for some hardware. the original file comes in an incorrect format with incorrect fields. so i pull the file and modify the fields and copy the certs. the issue is when my output file prints out it has the certs like this \n\n -----END CERTIFICATE----- ' but the portal where file will be uploaded expects both of the certs to print like this \n-----END CERTIFICATE-----\n' – Mbuyi Tuambilangana Commented Feb 14 at 17:23
  • Hi. Please edit your question and include an example of the raw certificate/key data: remove sensitive info if any, but leave the whitespace intact. Also, do you have the same problem for the pub-key files? – Zach Young Commented Feb 14 at 19:23
 |  Show 7 more comments

1 Answer 1

Reset to default 0

I 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?

发布评论

评论列表(0)

  1. 暂无评论