简体   繁体   English

如何摆脱事件和日期周围的方括号?

[英]How do I get rid of the square brackets around my event and date?

calendar = []
while True:
    choice = input ("enter a,b,c,d or e")
    if choice == "e":
        print ("you have quitted this program")
        break #to quit the entire program
    if choice == "a":
        newevent = input ("enter the name of an event you want to add")
        y= int(input("enter the year of your event"))
        m = int(input("enter the month of your event"))
        d = int(input("enter the day of your event"))
        h = int(input("enter the hour of your event"))
        mi = int(input("enter the minute of your event"))
        calendar.append ([newevent,y,m,d,h,mi])
        print (calendar)
        f = open("calendar.txt","w")
        f.write (str(calendar)+"\n")
        f.close()



f = open('calendar.txt','r')
st = f.readlines()
print (st)

for i in range (0,len(st)):
    st[i] = st[i].strip("\n")
    print (st)
    f.close()

This is my output:这是我的 output:

[['swim', 2020, 4, 2, 3, 2]]
["[['swim', 2020, 4, 2, 3, 2]]\n"]
["[['swim', 2020, 4, 2, 3, 2]]"]

I want to get rid of the square brackets around my code to make it swim,2020,4,2,3,2.我想去掉我的代码周围的方括号,让它游泳,2020,4,2,3,2。 Do I have to open the file before while True ? while True之前我必须打开文件吗?

For just stripping the end characters, use:对于剥离结束字符,请使用:

"[1, 2, 3]"[1:-1]

which will do just that.这将做到这一点。 Strip the first character from left and right.从左右剥离第一个字符。 But in case you want to save text or anything else, it'll require more work on your side to handle such special cases.但是,如果您想保存文本或其他任何内容,则需要您做更多的工作来处理这种特殊情况。

When saving you call f.write (str(calendar)+"\n") which if encounters a list does this:保存时调用f.write (str(calendar)+"\n")如果遇到list ,则执行以下操作:

[1, 2, 3]
    |
    v
"[1, 2, 3]"

if you want to convert back into Python list, simply use ast.literal_eval() on that via:如果您想转换回 Python 列表,只需通过以下方式使用ast.literal_eval()

from ast import literal_eval
mylist = literal_eval("[1, 2, 3]")
print(type(mylist))
print(mylist[0])

Or instead of plain str(object) use a better structure when saving to a file such as json .或者,在保存到诸如json类的文件时,而不是普通的str(object)使用更好的结构。

Use this code使用此代码

import json

calendar = []
while True:
    choice = input ("enter a,b,c,d or e : ")
    if choice == "e":
        print ("you have quitted this program")
        break #to quit the entire program
    if choice == "a":
        newevent = input ("enter the name of an event you want to add : ")
        y= int(input("enter the year of your event : "))
        m = int(input("enter the month of your event : "))
        d = int(input("enter the day of your event : "))
        h = int(input("enter the hour of your event : "))
        mi = int(input("enter the minute of your event : "))
        data = [newevent,y,m,d,h,mi]
        calendar.append(data)

with open("calendar.json","w") as f:
    f.write(f"{json.dumps(calendar)}")

with open('calendar.json','r') as f:
    st = f.read()
    j_data = json.loads(st)

    print(j_data)

Hy, since you want to remove the long brackets of the array. Hy,因为您想删除数组的长括号。 You can use the strip function.And remember to convert your array to a string if not aready For example:您可以使用条带 function。如果不方便,请记住将数组转换为字符串 例如:

str1="['a','b']"
str1=str1.strip('[]')
print(str1)

This will give a string 'a','b'.Hope this helps you这将给出一个字符串'a','b'。希望这可以帮助你

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

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