Python 2.7 指向文本插入python文件io
我需要一些代码,我一直在工作的帮助Python 2.7 指向文本插入python文件io,python-2.7,file-io,Python 2.7,File Io,我需要一些代码,我一直在工作的帮助 def themain(): print "============================================================================" lol = raw_input("""Would you like to add new stock prices to a company (add)/read all of the previous stock prices(read)(not
def themain():
print "============================================================================"
lol = raw_input("""Would you like to add new stock prices to a company (add)/read all of the previous
stock prices(read)(note that the newest stock price will be at the far right while
the oldest stock price will be located at the far left)/or add a new company(add new): """)
if lol.lower() == "add":
def main():
print "============================================================================"
lol = raw_input("Insert Name of Company: ")
print "Are you sure you typed the company name in right? Answer with yes or no. : " + lol
lol2 = raw_input()
if lol2 == "yes":
fo = open(lol.lower() + ".txt", "a")
main = raw_input("Type in new stock price: ")
fo.write(main + ":");
fo.close()
themain()
elif lol2 == "no":
main()
else:
print "Please type either yes or no"
themain()
main()
elif lol.lower() == "read":
def main2():
def read():
print "============================================================================"
lol = raw_input("Insert Name of Company: ")
try:
fo = open(lol.lower() + ".txt", "r+")
read = fo.read();
print read
fo.close()
except IOError:
print "Not a valid company please try again"
main2()
themain()
read()
main2()
elif lol.lower() == "add new":
def addnew():
print "============================================================================"
lol = raw_input("Insert the name of the new company: ")
print "Are you sure you typed in the name correctly?"
print lol
print "Please say either yes or no: "
response = raw_input()
if response.lower() == "yes":
fo = open(lol.lower() + ".txt", "a+")
fo.write(lol.lower + ":")
fo.close()
themain()
elif response.lower() == "no":
addnew()
else:
print "Please say either yes or no"
themain()
addnew()
else:
print "Please say either read, add new, or add"
themain()
themain()
我想粘贴代码后解释起来可能会更容易。因此,在创建新业务时,它会创建文件并将业务名称添加到文件的前面。然后,当你在业务中添加新股票时,它会将它们追加到末尾。例如,它将其放入文本文件Business:oldeststock:newerstock:neweststock。我想做的是让它按照这个顺序运行:neweststock:newerstock:oldeststock。我似乎找不到我该怎么做。我试着寻找,但当我对梅因这样做的时候,结果并不是很好
def main():
print "============================================================================"
lol = raw_input("Insert Name of Company: ")
print "Are you sure you typed the company name in right? Answer with yes or no. : " + lol
lol2 = raw_input()
if lol2 == "yes":
fo = open(lol.lower() + ".txt", "r+")
main = raw_input("Type in new stock price: ")
fo.seek(len(fo.name) - 4)
fo.write(":" + main);
fo.close()
themain()
elif lol2 == "no":
main()
else:
print "Please type either yes or no"
themain()
main()
我所做的是添加fo.seeklenfo.name-4,这样它将查找名称末尾减去扩展名.txt,但它所做的只是替换已经存在的数字。有没有什么方法可以添加到它,但从那一点开始,它向下移动其他数字并插入到那里,或者我完全认为这是错误的,有一个简单的解决方案,已经暗示了我三个小时;- 不是在r+模式下打开文本文件,而是在a+模式下打开文本文件并写入该文件是一种选择吗 所以看起来是这样的:
#Editing the if condition only
if lol2 == "yes":
#Indentation is off for some reason, this should be indented below the if statement
fo = open(lol.lower() + ".txt", "r+")
string = fo.read()
fo.close()
# Finding where the colon is located
index = string.find(":")
# Slicing where the :new:old is
aStr = string[index:]
main = raw_input("Type in new stock price: ")
# Putting the string slices together
main = string[:index]+":"+userinput+aStr
fo = open("test.txt","w+")
fo.write(main) #This would add the raw input at the end of the file.
fo.close()
themain()
你有没有考虑过。。。标准序列化选项?json、pickle、shelve等等?我从没听说过,但我可以去看看。顺便说一句,我不熟悉python。我不能使用pickle,因为我需要将它保存到txt文件中,因为java程序正在读取txt文件中的内容。然后使用json,这是事实上的跨平台序列化协议。或者使用lxml模块创建XML。java喜欢XML:在文件中间添加一些东西,你必须从文件中读取所有数据,并用新数据写入新文件。使用XML、JSON或CSV,读/写文件更容易。这会将文件添加到末尾,但我需要以下格式的文件:Business:neweststock:newerstock:oldeststock。那会是最新的。哎呀,我的错。我将编辑上面的代码。希望这有助于:-