简体   繁体   English

如何在python中进行个别输入?

[英]How to take individual input in python?

Calculator Language is what the problem is called and I am to code it in python. 计算器语言是问题的所在,我将在python中编写代码。 The coding part is done but I am having trouble while reading the input file. 编码部分已完成但我在读取输入文件时遇到问题。

So the input file looks like this : 所以输入文件如下所示:

A = B = 4
C = (D = 2)*_2
#

What i would like to do is to read each character, line by line ( each line is an expression and has to be calculated), characters as characters and integers as integers, since I push them into stacks. 我想要做的是逐行读取每个字符(每行是一个表达式,必须计算),字符作为字符,整数作为整数,因为我将它们推入堆栈。 There are two stacks one for the characters and numbers and the other for the operators. 有两个堆栈,一个用于字符和数字,另一个用于运算符。

Anyway this is what I have done with the input so far : 无论如何,这是我到目前为止所做的输入:

#!/usr/bin/python

a = open("testinput1.txt","r+")
wordList = [line.strip() for line in a];


print wordList[1]

And what i get is : 而我得到的是:

C = (D = 2)*_2

Also the end of file is reached when the file reader hits #. 当文件阅读器到达#时,也会到达文件末尾。

Any sort of help or suggestions are welcome. 欢迎任何形式的帮助或建议。

wordList is list of lines, each element is line (stripped one, without \\n') You should split each line, to get its tokens. wordList是行列表,每个元素都是行(剥离一个,没有\\ n')你应该拆分每一行,以获得它的标记。 Then for each token check if it is string or integer (using isdigit for example). 然后为每个令牌检查它是字符串还是整数(例如,使用isdigit)。

Now that your wordlist[0] contains your first statement, In python each and every string can be indexed directly without creating a seperate list for it. 现在你的wordlist [0]包含你的第一个语句,在python中,每个字符串都可以直接编入索引,而无需为它创建一个单独的列表。

for example: if wordlist[0] contains "c=a+b" , wordlist[0][0] will directly give you 'c'. 例如:如果wordlist [0]包含“c = a + b”,wordlist [0] [0]将直接给你'c'。

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

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