Python:在文本文件中将数据从垂直旋转到水平 [英] Python: Rotate data from vertical to horizontal in text file

查看:48
本文介绍了Python:在文本文件中将数据从垂直旋转到水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用python如何将文本文件中的数据从垂直对齐旋转到水平对齐.示例:

Using python how to rotate data in text file from vertical alignment to horizontal alignment. Example:

T
h
i
s

v
e
r
t
i
c
a
l

t
e
x
t

我希望它是这样的:

This is horizontal text

推荐答案

既然您知道输入中的每个字符都由 \n(换行符)字符分隔,那么您只需要做用空替换它们.

Since you know that every character in your input is separated by a \n (Line break) char, all you need to do is replace them with nothing.

如果您正在读取文件,则需要此代码:

If you are reading from a file you will need this code:

with open("PATH TO FILE.txt", r) as file:
    input = file.read()

要用空格替换 \n 字符,您需要:

To Replace \n characters with spaces you will need:

input.replace("\n", "")

现在只需结合两个代码片段

Now just combine the two code snippets

这篇关于Python:在文本文件中将数据从垂直旋转到水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆