psycopg2.errors.BadCopyFileFormat:最后一个预期列之后的额外数据,上下文:复制博客\u fbr\u数据,第155行:

2024-07-01 07:04:59 发布

您现在位置:Python中文网/ 问答频道 /正文

    import psycopg2
conn = psycopg2.connect(user="user_1",password="test123",host="127.0.0.1",port="5432",database="testdb")
cur = conn.cursor()
with open(r'/home/cybermakarov/Desktop/fbr/download_files/202033095128395ATL_IT.csv', mode='r', encoding='UTF-8', errors='strict', buffering=1) as f:
    # Notice that we don't need the `csv` module.
    #next(f) # Skip the header row.
    reader = csv.reader(f)
    next(reader)

    #cur.copy 'blog_fbr_data' FROM '/home/cybermakarov/Desktop/fbr/download_files/202033095128395ATL_IT.csv' );


    #database = \COPY ,'blog_fbr_data', from , var = '/home/cybermakarov/Desktop/fbr/download_files/202033095128395ATL_IT.csv', ' CSV HEADER DELIMITER', ',';

    cur.copy_from(f, 'blog_fbr_data', sep=',')
    conn.commit()
    conn.close()
    f.close()
    print("Success commit")

This is my code to insert data from .csv file, it contain more than 150,000 records and I find this solution to solve this problem but when I tried this solution and got the error on line number 155 because that column contain "," in some rows from CSV file which contain bulk data.

STATUS_CHOICES = (
    ('InActive', 'InActive'),
    ('Active', 'Active'),

)

Vendor_Type = (
    ('Compnay', 'Compnay'),
    ('Individual', 'Individual'),
)


class FBR_Data(models.Model):
    SR_NO = models.IntegerField(default=1)
    NTN_or_CNIC = models.BigIntegerField(primary_key=True)
    Name = models.CharField(max_length=100,  null=True, default=' ')
    Business_Name = models.CharField(max_length=200, null=True, default=' ' )
    #Extended_Business_Name = models.CharField(max_length=100, null=True, default= ' ')

    class Meta:
        verbose_name_plural = "FbrData"

    def __stf__(self):
        return self.FBR_Data

class Vendors_model(models.Model):
    Client_id = models.ForeignKey(AUTH_USER_MODEL, on_delete=models.PROTECT)
    Vendor_Number = models.IntegerField()
    Client_Vendor_Type = models.CharField(max_length=10, choices=STATUS_CHOICES, default='InActive')
    Client_Vendor_Name = models.CharField(max_length=50)
    Client_Vendor_NTN = models.IntegerField()
    Client_Vendor_Status = models.CharField(max_length=10,  choices=STATUS_CHOICES, default='InActive')
    ATL_NTN = models.ForeignKey(FBR_Data, on_delete = models.PROTECT, verbose_name='Fbr_Data', null=True)
    Is_approved = models.BooleanField(default=False)
    is_deleted = models.BooleanField(default=False)

    class Meta:
        verbose_name_plural = "Vendors"

    def __stf__(self):
        return self.Vendors_model





class Fbr_data_and_Vendors_Model(models.Model):
    Id = models.IntegerField()
    fbr_data = models.ForeignKey(FBR_Data, default=1, verbose_name='Fbr_Data', on_delete=models.SET_DEFAULT)
    vendors_model = models.ForeignKey(Vendors_model, default=1, verbose_name='Vendors', on_delete=models.SET_DEFAULT)

    class Meta:
        verbose_name_plural = "fbrData_and_VendorsModel"

    def __stf__(self):
        return self.Fbr_data_and_Vendors_Model

This is model,I tried to save null values as well but it did not worked for me. Please help me to solve this problem. Highly appreciate your contribution to solve this problem.Thank you in advance.


Tags: csvnameselfdefaultverbosedatamodelslength

热门问题