而从excel导入,+符号未插入数字字段,django

发布于 2025-02-14 01:37:45 字数 2543 浏览 0 评论 0原文

我有一个具有SMS-NO和WhatsApp-No字段的Excel文件。我们正在数据库中使用国家代码存储数字。虽然导入.xls文件编号已成功插入,但“+”符号未插入数据库中。

我正在共享Excel文件的屏幕截图以及如何将其存储在数据库中。

excel文件的屏幕截图

db

添加联系人选项

通过添加联系人选项在DB中的数据存储

这是我的ViewS.py

                fs = FileSystemStorage()
                filedata = fs.save("ImportDB.xls", myfile)
                BASE_DIR = os.path.dirname(
                    os.path.dirname(os.path.abspath(__file__)))
                filepath = BASE_DIR + "\media\ImportDB.xls"
                book = xlrd.open_workbook(filepath)
                sheet = book.sheet_by_name("Sheet1")
                database = MySQLdb.connect(
                    host="localhost", user="root", passwd="", db="mbs")
                cursor = database.cursor()
                query = """INSERT INTO addcontact (f_name, l_name, add_ln1, add_ln2, country, city, zip, sms_no, whtsp_no) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"""
                for r in range(1, sheet.nrows):
                        f_name = sheet.cell(r, 0).value
                        l_name = sheet.cell(r, 1).value
                        add_ln1 = sheet.cell(r, 2).value
                        add_ln2 = sheet.cell(r, 3).value
                        country = sheet.cell(r, 4).value
                        city = sheet.cell(r, 5).value
                        zip = sheet.cell(r, 6).value
                        sms_no = sheet.cell(r, 7).value
                        whtsp_no = sheet.cell(r, 8).value
                        values = (f_name, l_name, add_ln1, add_ln2, country, city, zip, sms_no, whtsp_no)
                        cursor.execute(query, values)
                cursor.close()
                database.commit()
                database.close()
                fs.delete(filedata)

成功插入的每个数据。但是在数据库中添加了没有 +符号的数字。

I have one excel file which has SMS-no and WhatsApp-no fields. We are storing numbers with country codes in our database. While importing .xls file number is inserted successfully but the '+' sign is not inserted in the database.

I am sharing a screenshot of the excel file and how it is stored in a database.

Screenshot of excel file
enter image description here

Screenshot of value store in DB

enter image description here

Add contact option

enter image description here

Data store in DB through Add Contact option
enter image description here

Here is my code of views.py

                fs = FileSystemStorage()
                filedata = fs.save("ImportDB.xls", myfile)
                BASE_DIR = os.path.dirname(
                    os.path.dirname(os.path.abspath(__file__)))
                filepath = BASE_DIR + "\media\ImportDB.xls"
                book = xlrd.open_workbook(filepath)
                sheet = book.sheet_by_name("Sheet1")
                database = MySQLdb.connect(
                    host="localhost", user="root", passwd="", db="mbs")
                cursor = database.cursor()
                query = """INSERT INTO addcontact (f_name, l_name, add_ln1, add_ln2, country, city, zip, sms_no, whtsp_no) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"""
                for r in range(1, sheet.nrows):
                        f_name = sheet.cell(r, 0).value
                        l_name = sheet.cell(r, 1).value
                        add_ln1 = sheet.cell(r, 2).value
                        add_ln2 = sheet.cell(r, 3).value
                        country = sheet.cell(r, 4).value
                        city = sheet.cell(r, 5).value
                        zip = sheet.cell(r, 6).value
                        sms_no = sheet.cell(r, 7).value
                        whtsp_no = sheet.cell(r, 8).value
                        values = (f_name, l_name, add_ln1, add_ln2, country, city, zip, sms_no, whtsp_no)
                        cursor.execute(query, values)
                cursor.close()
                database.commit()
                database.close()
                fs.delete(filedata)

Every data is inserted successfully. But number added without + sign in database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见 2025-02-21 01:37:45

您需要将字段从某些Integerfield更改为Chardfield。数据库将整数+123作为正数123

You need to change field from some IntegerField to ChardField. Database treats integer +123 as simply positive number 123.

戏剧牡丹亭 2025-02-21 01:37:45

我将数据转换为字符串和contrate +符号,但是在表达每个数字中,它在每个数字中附加了.0,因此我使用了拆分函数,并且仅获取值。 (点)。

这是我的代码

sms_no =  sheet.cell(r, 8).value
whtsp_no = sheet.cell(r, 9).value
sms1 = "+"+str(sms_no)
smsno = sms1.split(".")
finalsms = smsno[0]
whtsapp1 = "+"+str(whtsp_no)
whtsapno = whtsapp1.split(".")
finalwhtsap = whtsapno[0]

I converted data in string and concate + sign, but after concating it is appending .0 in each, number, So I used the split function and only get the value before. (dot).

here is my code

sms_no =  sheet.cell(r, 8).value
whtsp_no = sheet.cell(r, 9).value
sms1 = "+"+str(sms_no)
smsno = sms1.split(".")
finalsms = smsno[0]
whtsapp1 = "+"+str(whtsp_no)
whtsapno = whtsapp1.split(".")
finalwhtsap = whtsapno[0]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文