Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 将文件导入为列表\n在每一项之后_File_List_Python 3.x - Fatal编程技术网

File 将文件导入为列表\n在每一项之后

File 将文件导入为列表\n在每一项之后,file,list,python-3.x,File,List,Python 3.x,如果这个问题已经在这个论坛的其他地方得到了回答,请提前道歉,但我没有太多时间搜索 我在不同的文件中存储了许多列表,其中一个文件包含以下内容: 十三, 十三, 十, 但是,当我将文件作为列表导入到另一个程序中时,这可以正常工作: players = open("DATA/Players.txt").readlines() skills = open("DATA/Skills.txt").readlines() strengths = open("DATA/Strengths.txt").rea

如果这个问题已经在这个论坛的其他地方得到了回答,请提前道歉,但我没有太多时间搜索

我在不同的文件中存储了许多列表,其中一个文件包含以下内容:


十三,

十三,

十,


但是,当我将文件作为列表导入到另一个程序中时,这可以正常工作:

players = open("DATA/Players.txt").readlines()
skills = open("DATA/Skills.txt").readlines()
strengths = open("DATA/Strengths.txt").readlines()
每个项目后面都有(\n)/(换行符),如下所示

['13\n', '13\n', '10\n']
当我导入列表时,如何消除此问题?我相信一定有一个简单的解决办法。
感谢您的帮助。

使用<代码>条带:

>>> a = 'b\n'
>>> a.strip()
'b'
或者更恰当地说,
rstrip()
,如果您的
\n
仅显示在字符串的右侧:

>>> a.rstrip()
'b'
在列表中:

>>> l = ['a\n', 'b\n', 'c\n']
>>> no_ns = [var.rstrip() for var in l]
>>> no_ns
['a', 'b', 'c']

使用
条带

>>> a = 'b\n'
>>> a.strip()
'b'
或者更恰当地说,
rstrip()
,如果您的
\n
仅显示在字符串的右侧:

>>> a.rstrip()
'b'
在列表中:

>>> l = ['a\n', 'b\n', 'c\n']
>>> no_ns = [var.rstrip() for var in l]
>>> no_ns
['a', 'b', 'c']

使用
条带

>>> a = 'b\n'
>>> a.strip()
'b'
或者更恰当地说,
rstrip()
,如果您的
\n
仅显示在字符串的右侧:

>>> a.rstrip()
'b'
在列表中:

>>> l = ['a\n', 'b\n', 'c\n']
>>> no_ns = [var.rstrip() for var in l]
>>> no_ns
['a', 'b', 'c']

使用
条带

>>> a = 'b\n'
>>> a.strip()
'b'
或者更恰当地说,
rstrip()
,如果您的
\n
仅显示在字符串的右侧:

>>> a.rstrip()
'b'
在列表中:

>>> l = ['a\n', 'b\n', 'c\n']
>>> no_ns = [var.rstrip() for var in l]
>>> no_ns
['a', 'b', 'c']