执行附加操作后,目标文件中看到的行为不一致
在附加文件的条目时,我面临一个奇怪的问题。
关于代码的一些背景,它是一组Python模块,该模块,
1st模块负责比较两个CSV文件[Master Records和最新记录和最新记录::这两个文件包含具有某些属性的对象],从两个文件并调用第二个模块以使用API在Web服务器上创建/修改/删除某些对象。
第二个模块负责使用Python“请求”库通过API与Web服务器进行交互。此模块更新[add/new/delete] Master Record CSV文件中的条目基于该记录采取的操作。
在末尾,第一个模块在这些对象上打印出Delta对象记录和操作到终端,如下所示,
+----+-----------+---------------+----------------+------------+----------+--------------------------------------+--------------------------------------------+
| | UE_NAME | UE_TYPE | ES_NAME | APN_NAME | STATUS | UE_ROUTES_OPERATION(MOBILE_ROUTER) | UE_ROUTES(MOBILE_ROUTER) |
+====+===========+===============+================+============+==========+======================================+============================================+
| 0 | ue_1 | MOBILE_ROUTER | Edge_service_4 | apn1862 | UE_ADDED | UE_ROUTE_ADDED | ['172.16.10.0/26(A)'] |
+----+-----------+---------------+----------------+------------+----------+--------------------------------------+--------------------------------------------+
| 1 | ue_6 | IOT_MOBILE | Edge_service_1 | apn_nuage | UE_ADDED | -- | -- |
+----+-----------+---------------+----------------+------------+----------+--------------------------------------+--------------------------------------------+
| 2 | ue_2 | MOBILE_ROUTER | Edge_service_4 | apn1862 | UE_ADDED | UE_ROUTE_ADDED | ['172.16.10.0/18(A)', '172.16.12.0/24(A)'] |
+----+-----------+---------------+----------------+------------+----------+--------------------------------------+--------------------------------------------+
| 3 | ue_201 | MOBILE_ROUTER | test1 | apn_nuage | UE_ADDED | UE_ROUTE_ADDED | ['172.16.10.0/16 (A)'] |
+----+-----------+---------------+----------------+------------+----------+--------------------------------------+--------------------------------------------+
现在,ACK陷入问题,从代码角度来看,一切正常。但是,我看到更新附加到Master_Record文件的问题。在最初执行此工具之后,可以看到Master Record CSV,
more .\master_records.csv
name,IMSI,MSISDN,ICCID,IMEI,Enterprise,operationalStatus,ueType,ueRoutes,description,apn_name
ue_1,1,1,1,1,Edge_service_4,IDLE,MOBILE_ROUTER,"172.16.10.0/26",UE-1-desc,apn1862
ue_6,6,6,6,6,Edge_service_1,CONNECTED,IOT_MOBILE,,UE-6,apn_nuage
ue_2,2,2,2,2,Edge_service_4,CONNECTED,MOBILE_ROUTER,"172.16.10.0/18,172.16.12.0/24",UE-2-desc,apn1862
ue_201,201,201,201,203,test1,IDLE,MOBILE_ROUTER,"172.16.10.0/16 ",UE-201,apn_nuage
但是在修改了“最新_records.csv”中的其中一个条目[例如 ue_1 ]之后,从对象记录中丢失了双引号 ue_201 ,脚本完成后,master_records文件站在脚本上,
more .\master_records.csv
name,IMSI,MSISDN,ICCID,IMEI,Enterprise,operationalStatus,ueType,ueRoutes,description,apn_name
ue_6,6,6,6,6,Edge_service_1,CONNECTED,IOT_MOBILE,,UE-6,apn_nuage
ue_2,2,2,2,2,Edge_service_4,CONNECTED,MOBILE_ROUTER,"172.16.10.0/18,172.16.12.0/24",UE-2-desc,apn1862
ue_201,201,201,201,203,test1,IDLE,MOBILE_ROUTER,172.16.10.0/16 ,UE-201,apn_nuage
ue_1,1,1,1,1,Edge_service_4,CONNECTED,MOBILE_ROUTER,"172.16.10.0/26",UE-1-desc,apn1862
您可以看到对象围绕172.16.10.0/16丢失了双引号, ue_201
此问题是仅在“ ueroutes”列下只有一个对象的情况下才能看到,如果我有多个对象,例如“ 172.16.10.0/18,172.16.16.12.0/24”,我看不到这个问题。
更新主记录的代码非常简单如下所示,
def add_entry_master_record(row_entry):
"""
Adds a UE Entry to Master Record file.
:param row_entry: UE record Entry.
:return: Updates the existing Master Record file.
"""
try:
with open(m_record, 'a+') as master_record_update:
master_record_update.write(row_entry)
master_record_update.write('\n')
except FileNotFoundError as err_file:
raise SystemExit(err_file)
在检查“ row_entry”的内容后,我看不到任何更新到ue_201的master_record文件,并且仅适用于下面的ue_1,
row_entry = ue_1,1,1,1,1,Edge_service_4,CONNECTED,MOBILE_ROUTER,"172.16.10.0/26",UE-1-desc,apn1862
一旦这种不规则性在,它开始弄乱我的代码,因为最新_record和master_record文件之间的三角洲计算也将not-delta内容列出。在比较之前,我可以通过使用一些正则替换来解决这个问题,但是我仍然对这种不规则性首先发生的原因感到好奇。
关于如何更新master_record文件的注释:
- 我正在使用以下代码,从而在使用较早共享的附录代码修改现有对象后,将现有对象的old_record和新记录删除,
def drop_rows_master_record(ue_name, mod_entry):
"""
Drops the Entries from Master Record csv file.
:param ue_name: UE Name for which Entry needs to be dropped.
:param mod_entry: UE Record
:return: Drops the deleted/modified UE record from Master Records.
"""
df = pd.read_csv(m_record)
df_filtered = df[(df['name'].str.lower() == ue_name.lower()) &
(df['Enterprise'].str.lower() == mod_entry['Enterprise'].lower())]
df.drop(df_filtered.index, inplace=True)
df.reset_index(drop=True, inplace=True)
df.to_csv(m_record, index=False)
如果您知道,请告诉我人们可以首先分享这个差异为什么蔓延的指针。
I am facing a strange issue while appending the entries to a File.
A bit of background about code, it's a set of Python Modules whereby,
1st module is responsible to compare two CSV files[Master Records and Latest Records :: These two file contains Objects with certain attributes], pull out the deltas between two files and Call the 2nd module to create/modify/delete certain objects on a Webserver using APIs.
2nd Module is responsible to interact with Webserver over API using Python 'requests' library. This module updates[add/new/delete] the entries in Master record CSV file based on the action that it took for a certain record.
At the end, 1st Module prints out the delta Object records and operations done on those objects to the Terminal, something like below,
+----+-----------+---------------+----------------+------------+----------+--------------------------------------+--------------------------------------------+
| | UE_NAME | UE_TYPE | ES_NAME | APN_NAME | STATUS | UE_ROUTES_OPERATION(MOBILE_ROUTER) | UE_ROUTES(MOBILE_ROUTER) |
+====+===========+===============+================+============+==========+======================================+============================================+
| 0 | ue_1 | MOBILE_ROUTER | Edge_service_4 | apn1862 | UE_ADDED | UE_ROUTE_ADDED | ['172.16.10.0/26(A)'] |
+----+-----------+---------------+----------------+------------+----------+--------------------------------------+--------------------------------------------+
| 1 | ue_6 | IOT_MOBILE | Edge_service_1 | apn_nuage | UE_ADDED | -- | -- |
+----+-----------+---------------+----------------+------------+----------+--------------------------------------+--------------------------------------------+
| 2 | ue_2 | MOBILE_ROUTER | Edge_service_4 | apn1862 | UE_ADDED | UE_ROUTE_ADDED | ['172.16.10.0/18(A)', '172.16.12.0/24(A)'] |
+----+-----------+---------------+----------------+------------+----------+--------------------------------------+--------------------------------------------+
| 3 | ue_201 | MOBILE_ROUTER | test1 | apn_nuage | UE_ADDED | UE_ROUTE_ADDED | ['172.16.10.0/16 (A)'] |
+----+-----------+---------------+----------------+------------+----------+--------------------------------------+--------------------------------------------+
Now, coming ack to problem, everything is working fine from code perspective. But, I see an issue with updates getting appended to Master_record file. After initial execution of this Tool, Following Master record CSV was seen,
more .\master_records.csv
name,IMSI,MSISDN,ICCID,IMEI,Enterprise,operationalStatus,ueType,ueRoutes,description,apn_name
ue_1,1,1,1,1,Edge_service_4,IDLE,MOBILE_ROUTER,"172.16.10.0/26",UE-1-desc,apn1862
ue_6,6,6,6,6,Edge_service_1,CONNECTED,IOT_MOBILE,,UE-6,apn_nuage
ue_2,2,2,2,2,Edge_service_4,CONNECTED,MOBILE_ROUTER,"172.16.10.0/18,172.16.12.0/24",UE-2-desc,apn1862
ue_201,201,201,201,203,test1,IDLE,MOBILE_ROUTER,"172.16.10.0/16 ",UE-201,apn_nuage
But, After modifying one of the entries[say, ue_1] in "Latest_records.csv", double-quotes went missing from the Object records of ue_201, and after completion of script, master_records file stood at,
more .\master_records.csv
name,IMSI,MSISDN,ICCID,IMEI,Enterprise,operationalStatus,ueType,ueRoutes,description,apn_name
ue_6,6,6,6,6,Edge_service_1,CONNECTED,IOT_MOBILE,,UE-6,apn_nuage
ue_2,2,2,2,2,Edge_service_4,CONNECTED,MOBILE_ROUTER,"172.16.10.0/18,172.16.12.0/24",UE-2-desc,apn1862
ue_201,201,201,201,203,test1,IDLE,MOBILE_ROUTER,172.16.10.0/16 ,UE-201,apn_nuage
ue_1,1,1,1,1,Edge_service_4,CONNECTED,MOBILE_ROUTER,"172.16.10.0/26",UE-1-desc,apn1862
You can see that double quotes around the 172.16.10.0/16 are missing for object, ue_201
This issue is seen only in the instances where there is only one object under 'ueRoutes' column, If I have multiple Objects like "172.16.10.0/18,172.16.12.0/24", I don't see this issue.
Code to update the Master Records is very simple as marked below,
def add_entry_master_record(row_entry):
"""
Adds a UE Entry to Master Record file.
:param row_entry: UE record Entry.
:return: Updates the existing Master Record file.
"""
try:
with open(m_record, 'a+') as master_record_update:
master_record_update.write(row_entry)
master_record_update.write('\n')
except FileNotFoundError as err_file:
raise SystemExit(err_file)
Upon checking the contents of 'row_entry', I don't see any updates going to Master_record file for ue_201 and it was only for ue_1 something like below,
row_entry = ue_1,1,1,1,1,Edge_service_4,CONNECTED,MOBILE_ROUTER,"172.16.10.0/26",UE-1-desc,apn1862
Once this irregularity creeps in, it starts messing with my code as the delta calculations between latest_record and master_record file dishes out the not-delta content as well. I was able to work-around this issue by using some regex substitutions before comparison, but I am still curious about why this irregularity happens in first place.
A note on how I update the Master_record file:
- I am using following piece of code whereby I drop the old_record for an existing object and add-in new record after modifying the existing object using the append code shared earlier,
def drop_rows_master_record(ue_name, mod_entry):
"""
Drops the Entries from Master Record csv file.
:param ue_name: UE Name for which Entry needs to be dropped.
:param mod_entry: UE Record
:return: Drops the deleted/modified UE record from Master Records.
"""
df = pd.read_csv(m_record)
df_filtered = df[(df['name'].str.lower() == ue_name.lower()) &
(df['Enterprise'].str.lower() == mod_entry['Enterprise'].lower())]
df.drop(df_filtered.index, inplace=True)
df.reset_index(drop=True, inplace=True)
df.to_csv(m_record, index=False)
Let me know, if you guys can share a pointer on why this discrepancy is creeping in, in first place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了这个问题的根源。这不是熊猫或python的问题。 :)
Pandas只是确认CSV格式,如果列内定义了一个元素,则删除引号。
因此,例如,如果一个CSV列
在文本文件中只有一个值,我们将看到它而无需引号。
但是,如果CSV列有多个值,则
在文本文件中,我们将用引号看到它。
I found the root of this issue. It wasn't an issue with Pandas or Python. :)
Pandas was just confirming to the CSV format and removing the quotes if there was a single element defined inside a column.
So, for example, if a CSV column has only one Value like
in text file, we will see it without quotes.
But, if there are multiple values for a CSV column,
in text file, we will see it with quotes.