python 在多行上打印字符串
我有一个只能接受字符串的函数。 (它用字符串创建图像,但字符串几乎没有格式,也没有自动换行,因此长字符串会直接穿过图像边缘并继续进入深渊,而实际上我希望它创建一个段落,而不是一行无限)。
我需要它打印时带有换行符。当前正在使用该文件进行读取,
inputFiles.readlines()
以便读取整个文件。存储 file.readLines() 创建一个列表。所以这个列表不能传递给我寻找字符串的函数。
我
inputFileContent = ' \n'.join(inputFiles.readLines())
试图强制在每个列表项之间的字符串中强制换行。这不起作用(编辑:此处详细说明),这意味着 inputFileContent 字符串没有换行符,即使我在列表元素之间放置 '\n' 也是如此。根据我的理解, readLines() 函数将各个行放入列表的各个元素中。
有什么建议吗?谢谢
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. 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. From my understanding, the readLines() function puts the individual lines into individual elements of a list.
any suggestions? Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
inputFiles.read()
创建一个字符串。这有帮助吗?Use
inputFiles.read()
which creates a string. Does that help?“加入”应该有效。您的问题可能是字符串的写入忽略换行符。你也许可以尝试 '\r\n'.join(...)
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(...)