当我尝试从文件中分割字符串时,如果将其保存到Python列表中,会将空格作为元素

我想从文件中获取数据,并将其整理成如下形式的元组列表:

[('yes', 31, 'good'), ('yes', 31, 'good'), ('yes', 31, 'good')]

我的文件看起来像这样

我的文件图片

问题是当我用换行符(\n)分割时,结果会在列表中每个字符串后添加一个包含空格的元素,如下输出所示

我在这里所做的是将它分割到一个列表中

我想知道为什么在列表中每个字符串后会得到一个空格作为元素

with open('c:\\users\\Ahmed Zaky\\Desktop\\Ahmed\\Master\\TUB\\PyML\\Lecture 1 15 Oktober - 21 Oktober\\Exercise Sheet 1\\health-test.txt','r') as h_test:D = list()for t in h_test:    D.extend([x for x in str.split(t,'\n')])print(D)

输出

['yes,21,poor', '', 'no,50,good', '', 'no,23,good', '', 'yes,45,poor', '', 'yes,51,good', '', 'no,60,good', '', 'no,15,poor', '', 'no,18,good', '']

元组列表看起来像这样

with open('c:\\users\\Ahmed Zaky\\Desktop\\Ahmed\\Master\\TUB\\PyML\\Lecture 1 15 Oktober - 21 Oktober\\Exercise Sheet 1\\health-test.txt','r') as h_test:D = list()for t in h_test:    D.extend([(x,) for x in str.split(t,'\n')])print(D)

输出

[('yes,21,poor',), ('',), ('no,50,good',), ('',), ('no,23,good',), ('',), ('yes,45,poor',), ('',), ('yes,51,good',), ('',), ('no,60,good',), ('',), ('no,15,poor',), ('',), ('no,18,good',), ('',)]

回答:

不要用换行符split,而是先用strip去除行的空白字符。然后用逗号split你得到的字符串,并将第二个值转换为int

with open(path) as h_test:    stripped_lines = (line.strip() for line in h_test)    non_empty_lines = (line for line in stripped_lines if line)    split_lines = (line.split(',') for line in non_empty_lines)    D = [(a, int(b), c) for a, b, c in D]

如果有行不包含三个值,且第二个值不是int,这将失败。根据您的数据,您可能需要对此进行检查。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注