简体   繁体   English

如何将从文件读取的行转换为一个长字符串?

[英]How to convert lines read from a file into one long string?

I am trying to create a postfix calculator. 我正在尝试创建一个后缀计算器。 I have a file named expressions.txt that comes with six postfix operations. 我有一个名为expressions.txt的文件,它带有六个后缀操作。 When I read the files in, it gives me a list with a bunch of new lines. 当我读入文件时,它会给我一个包含一堆新行的列表。 For example, 例如,

f = open("expressions.txt", "r")
expression = f.readlines()

gives me: 给我:

['5 4 3 + 2 * -\n', '8 5 *\n', '20 5 /\n', '3 8 6 + *\n', '3 4 + 9 - 12 +\n', '9 3 2 1 + + /\n', '3 + 4\n', '* 3 4 5 + *\n', '4 9 1 3 + -\n', 'h 3 +\n']

I need 我需要

['5 4 3 + 2 * 8 5 * 20 5 / 3 8 6 ... ]

and so on and so on. 等等等等。 I am trying to figure out how to instead of get each line into a list, i need each line to be joined into one big string. 我试图弄清楚如何而不是将每一行都放入一个列表,而是需要将每一行都连接成一个大字符串。

EDIT: Okay, here is the full code. 编辑:好的,这是完整的代码。

from ArrayStack import *

evaluation = Stack()

def main():
   count = 0
   f = open("expressions.txt", "r")
   l = f.readlines()
   for char in l:
     char.replace("\n","")
     char.split(' ')
     evaluation.push(char)
   print(evaluation.data)

It is still not working to where I can push each number and operation onto the stack. 我仍然无法将每个数字和操作压入堆栈。

If you can control the input file, this is easiest with tr on unix: 如果您可以控制输入文件,那么在unix上使用tr最简单:

tr '\n' '' < input_file

If you have to use python, this will work: 如果必须使用python,则可以使用:

with open('file') as f:
  expression = f.read().replace('\n', '')

Notice that I used f.read() instead of f.readlines() . 注意,我使用了f.read()而不是f.readlines() f.read() returns a string rather than a list of strings, which saves you re-joining the lines later. f.read()返回一个字符串而不是一个字符串列表,这样f.read()您以后重新加入这些行。

>>> l = ['5 4 3 + 2 * -\n', '8 5 *\n', '20 5 /\n', '3 8 6 + *\n', '3 4 + 9 - 12 +\n', '9 3 2 1 + + /\n', '3 + 4\n', '* 3 4 5 + *\n', '4 9 1 3 + -\n', 'h 3 +\n']
>>> s ="".join(i.replace("\n","") for i in l)
'5 4 3 + 2 * -8 5 *20 5 /3 8 6 + *3 4 + 9 - 12 +9 3 2 1 + + /3 + 4* 3 4 5 + *4 9 1 3 + -h 3 +'

Also if you want to take it one step further to prepare for parsing (if that's what you're going for) you can do this 另外,如果您想更进一步以准备分析(如果要这样做),则可以执行此操作

>>> s.replace(" ","")
'543+2*-85*205/386+*34+9-12+9321++/3+4*345+*4913+-h3+'
expressions = "".join(line.rstrip("\n") for line in open("expressions.txt", "r"))
expression = ['5 4 3 + 2 * -\n', '8 5 *\n', '20 5 /\n', '3 8 6 + *\n', '3 4 + 9 - 12 +\n', '9 3 2 1 + + /\n', 
          '3 + 4\n',
        '* 3 4 5 + *\n', '4 9 1 3 + -\n', 'h 3 +\n']

expression = [''.join([char for char in ''.join(expression) if char != '\n'])]

Output: 输出:

["5 4 3 + 2 * -8 5 *20 5 /3 8 6 + *3 4 + 9 - 12 +9 3 2 1 + + /3 + 4* 3 4 5 + *4 9 1 3 + -h 3 +"]

Instead of replace , strip can serve you too: 除了replacestrip也可以为您服务:

with open("expressions.txt") as f:
    expression = "".join(line.strip("\n") for line in f)

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

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