Python 从字符串到元组的转换-双逗号
我想从文件中读取行,并在元组列表中打印内容。Python 从字符串到元组的转换-双逗号,python,string,file,tuples,Python,String,File,Tuples,我想从文件中读取行,并在元组列表中打印内容。 但在转换过程中,我得到了两个逗号。 我很难找到删除逗号的方法,这更重要 代码: def arrivalsFile(file_name): """ Reads part of an input file with the arrivals into a list of flights. Requires: file_name, for arrivals, is a text file with the structure ind
但在转换过程中,我得到了两个逗号。
我很难找到删除逗号的方法,这更重要 代码:
def arrivalsFile(file_name):
"""
Reads part of an input file with the arrivals into a list of flights.
Requires: file_name, for arrivals, is a text file with the structure indicated in the quizz
Ensures: list of tuples, each corresponding to one flight
>>> arrivalsFile("arrivals_14_16.txt")
[('KLM75', 'Amsterdam', '14:35', '60', '50'), ('AF111', 'Paris', '14:20', '50', '64'), ('LH333', 'Frankfurt', '14:10', '112', '203'), ('KLM71', 'Madrid', '14:55', '120', '100'), ('TAP103', 'Salvador', '15:20', '174', '210'), ('LH123', 'Berlin', '15:10', '115', '210')]
"""
lista = []
inFile = open(file_name, "r")
for line in inFile:
if "Arrivals:" in line:
for line in inFile:
lista.append(tuple(line.split()))
inFile.close()
执行:
**********************************************************************
File "Z:\Documents\1415\airConveyorBeltsGroup11\readInput.py", line 9, in __main__.arrivalsFile
Failed example:
arrivalsFile("arrivals_14_16.txt")
Expected:
[('KLM75', 'Amsterdam', '14:35', '60', '50'), ('AF111', 'Paris', '14:20', '50', '64'), ('LH333', 'Frankfurt', '14:10', '112', '203'), ('KLM71', 'Madrid', '14:55', '120', '100'), ('TAP103', 'Salvador', '15:20', '174', '210'), ('LH123', 'Berlin', '15:10', '115', '210')]
Got:
[('KLM75,', 'Amsterdam,', '14:35,', '60,', '50'), ('AF111,', 'Paris,', '14:20,', '50,', '64'), ('LH333,', 'Frankfurt,', '14:10,', '112,', '203'), ('KLM71,', 'Madrid,', '14:55,', '120,', '100'), ('TAP103,', 'Salvador,', '15:20,', '174,', '210'), ('LH123,', 'Berlin,', '15:10,', '115,', '210')]
**********************************************************************
1 items had failures:
1 of 1 in __main__.arrivalsFile
***Test Failed*** 1 failures.
按
,
拆分,而不是按空格
,拆分。您需要在,
处拆分
>>> line = 'KLM75, Amsterdam, 14:35, 60, 50'
>>> print(tuple(line.split(',')))
('KLM75', ' Amsterdam', ' 14:35', ' 60', ' 50')
如果需要去掉前导空格和尾随空格,请使用以下命令
>>> print(tuple(element.strip() for element in line.split(',')))
('KLM75', 'Amsterdam', '14:35', '60', '50')
根据您的评论,您不想使用列表理解。从技术上讲,我使用了生成器表达式,而不是列表理解,但我修改了您的代码以显示您想要的内容
lista = []
inFile = open(file_name, "r")
for line in inFile:
if "Arrivals:" in line:
data = []
for line in inFile:
for element in line.split(','):
data.append(element.strip())
lista.append(tuple(data))
inFile.close()
对于每一行,我们创建一个名为
data
的新列表,将剥离的元素附加到此列表中,并在循环行中的所有元素后将此列表转换为元组。print(tuple(element.strip(),用于line.split(','))-我如何在不理解的情况下编写此代码?@Miguel Luís:检查我的更改。这就是您想要的吗?您可以使用行拆分项目并删除多余的空格。拆分(“,”)
@martineau:如果行中没有双空格,您的解决方案将有效。即使您只使用line='KLM75,阿姆斯特丹,14:35,60,50'
也会在末尾留下多余的空间。@Matthia很抱歉今天才回复。是的,这正是我想要的,但与我使用生成器表达式时的工作方式不同。
lista = []
inFile = open(file_name, "r")
for line in inFile:
if "Arrivals:" in line:
data = []
for line in inFile:
for element in line.split(','):
data.append(element.strip())
lista.append(tuple(data))
inFile.close()