简体   繁体   English

python多行打印字符串

[英]python print string on multiple lines

I have a function that can only accept strings. 我有一个只能接受字符串的函数。 (it creates the image with the string, but the string has little formatting and no word wrapping, so a long string will just bleed right through the edge of the image and keep going into the abyss, when in reality I would have liked it to create a paragraph, instead of a one line infinity). (它使用字符串创建图像,但是字符串的格式很少,并且没有自动换行,因此长字符串只会在图像的边缘流血并不断进入深渊,而实际上我本来希望它能够创建一个段落,而不是一行无限。

I need it print with line breaks. 我需要带有换行符的打印。 Currently the file is being readin using 当前正在使用读取文件

inputFiles.readlines()

so that this reads the entire file. 这样就可以读取整个文件。 Storing file.readLines() creates a list. 存储file.readLines()创建一个列表。 So this list cannot be passed to my function looking for a string. 因此,此列表无法传递给我的函数以查找字符串。

I used 我用了

inputFileContent = ' \n'.join(inputFiles.readLines())

in an attempt to force hard line breaks into the string between each list item. 试图强行插入每个列表项之间的字符串。 This does not work (edit: elaboration here) which means that the inputFileContent string does not have line breaks even though I put '\\n' between the list elements. 这不起作用(编辑:此处详细说明),这意味着即使我在列表元素之间放置了'\\ n',inputFileContent字符串也没有换行符。 From my understanding, the readLines() function puts the individual lines into individual elements of a list. 据我了解,readLines()函数将各行放入列表的各个元素中。

any suggestions? 有什么建议么? Thank you 谢谢

Use inputFiles.read() which creates a string. 使用inputFiles.read()创建一个字符串。 Does that help? 有帮助吗?

The 'join' should have worked. “联接”应该起作用了。 Your problem may be that the writing of the string ignores newline characters. 您的问题可能是字符串的编写忽略了换行符。 You could maybe try '\\r\\n'.join(...) 您可以尝试使用'\\ r \\ n'.join(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM