Python代码在Windows和CentOS中的工作方式不同



我有一个python代码,当我在Windows上运行它时,它呈现出不同的行为,而当我在CentOS上运行它。以下是本期感兴趣的部分代码,并附有注释以解释其目的。它基本上处理一堆CSV文件(其中一些文件具有不同的列(,并将它们合并为一个具有所有列的CSV:

#Get the name of CSV files of the current folder:
local_csv_files = glob("*.csv")
#Define the columns and the order they should appear on the final file:
global_csv_columns = ['Timestamp', 'a_country', 'b_country', 'call_setup_time','quality','latency','throughput','test_type']
#Dataframe list:
lista_de_dataframes=[]

#Loop to be executed for all the CSV files in the current folder.
for ficheiro_csv in local_csv_files:
df = pd.read_csv(ficheiro_csv)
#Store the CSV columns on a variable and collect the number of columns:
colunas_do_csv_aux= df.columns.values
global_number_of_columns = len(global_csv_columns)
aux_csv_number_of_columns = len(colunas_do_csv_aux)
#Normalize each CSV file so that all CSV files have the same columns
for coluna_ in global_csv_columns:
if search_column(colunas_do_csv_aux, coluna_)==False:
#If the column does not exist in the current CSV, add an empty column with the correct header:
df.insert(0, coluna_, "")
#Order the dataframe columns according to the order of the global_csv_columns list:
df = df[global_csv_columns]
lista_de_dataframes.append(df)
del df
big_unified_dataframe = pd.concat(lista_de_dataframes, copy=False).drop_duplicates().reset_index(drop=True)
big_unified_dataframe.to_csv('global_file.csv', index=False)
#Create an additional txt file to present with each row of the CSV in a JSON format:
with open('global_file.csv', 'r') as arquivo_csv:
with open('global_file_c.txt', 'w') as arquivo_txt:
reader = csv.DictReader(arquivo_csv, global_csv_columns)
iterreader = iter(reader)
next(iterreader)
for row in iterreader:
out=json.dumps(row)
arquivo_txt.write(out)

现在,在Windows和CentOS上,这对最终的CSV非常有效,因为它按照列表中的定义对所有列进行了排序:

global_csv_columns = ['Timestamp', 'a_country', 'b_country', 'call_setup_time','quality','latency','throughput','test_type']

此订购通过以下代码行实现:

#Order the dataframe columns according to the order of the global_csv_columns list:
df = df[global_csv_columns]

但最终的"txt"文件在CentOS上有所不同。在CentOS中更改顺序。下面是两个平台(windows和CentOS(中txt文件的输出。

窗口

{"Timestamp": "06/09/2022 10:33", "a_country": "UAE", "b_country": "UAE", "call_setup_time": "7.847", "quality": "", "latency": "", "throughput": "", "test_type": "voice_call"}
{"Timestamp": "06/09/2022 10:30", "a_country": "Saudi_Arabia", "b_country": "Saudi_Arabia", "call_setup_time": "10.038", "quality": "", "latency": "", "throughput": "", "test_type": "voice_call"}
...

Cents:

{"latency": "", "call_setup_time": "7.847", "Timestamp": "06/09/2022 10:33", "test_type": "voice_call", "throughput": "", "b_country": "UAE", "a_country": "UAE", "quality": ""}
{"latency": "", "call_setup_time": "10.038", "Timestamp": "06/09/2022 10:30", "test_type": "voice_call", "throughput": "", "b_country": "Saudi_Arabia", "a_country": "Saudi_Arabia", "quality": ""}
...

有什么方法可以保证在CentOS中的列顺序吗?

在CentOS上运行:Python 2.7.18在Windows上运行:Python3.9.6

现在原因很清楚了:公共dict内部的顺序是在python3.6中添加的,作为特定于实现的,并且需要在python2.7及更新版本中提供。

阅读Python 3.6+中的词典是有序的吗?如果你想了解更多。

如果您知道我应该使用哪个命令/版本/存储库来安装类似的版本在CentOS上,请告诉我。

最佳解决方案是使用相同的python版本,最高可达minor版本,也就是说,如果您的Windows机器上有3.9.6版本,那么在CentOS上有python3.9版本。如果你无法安装它,python3.7或python3.8应该这样做,但请注意,如果你在一台机器上同时安装了python2和python3,那么如果你想使用更新的版本,那么你应该使用python3

python3 helloworld.py

其中helloworld.py是带有python代码的文件。

尝试pd.DataFrame.to_json函数,该函数允许您将数据帧直接写入json文件。这将允许您将df写入json文件,而无需从csv文件中读取。我怀疑这个函数可能允许您在不更改列顺序的情况下进行编写。

您的输出JSON字典没有排序,因此标记的出现顺序可能是随机的。我认为在实践中,标签通常按照它们在每个字典中创建的顺序出现,但如果你可以按标签对字典进行排序:

out=json.dumps(row, sort_keys=True)

这至少会使它们保持一致,尽管你可能会在一些标签上赋予更多的含义。

最新更新