Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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
Python suppress/n位于txt文件的末尾_Python_Text_Txt - Fatal编程技术网

Python suppress/n位于txt文件的末尾

Python suppress/n位于txt文件的末尾,python,text,txt,Python,Text,Txt,我有以下代码: with open(path, 'r') as f: data = f.readlines() print("x "+ data[1] + "y") data[1]是txt文件中的字符串。我得到的结果是: >>> x 10 >>> y 但我希望“y”与“x10”在同一行。可以用Python来做吗 提前谢谢你 请注意,数据[1]是txt文件中的字符串不准确。数据[1]是文件的第二行

我有以下代码:

with open(path, 'r') as f:
    data = f.readlines()
    print("x "+ data[1] + "y")
data[1]
是txt文件中的字符串。我得到的结果是:

>>> x 10
>>> y
但我希望“y”与“x10”在同一行。可以用Python来做吗


提前谢谢你

请注意,
数据[1]是txt文件中的字符串
不准确。数据[1]是文件的第二行

    data = [line.rstrip() for line in f.readlines()]

听起来您想从数据[1]中删除“\n”

你可以在这个链接上找到它。

您可以使用替换

>>>test = "the\n line"

>>>print(test)
the
 line

>>>print(test.replace("\n",""))
the line


“是否可以在Python中执行此操作?”-是的。请查看索引
数据[1][:-1]
中可以轻松删除最后一个尾行字符的位置。这是否回答了您的问题?