简体   繁体   English

如何从 Python 3 中的磁盘读取和编辑列表?

[英]How to read and edit a list from disk in Python 3?

I have a list of dictionaries that I want to append to a separate disk file.我有一个字典列表,我想将 append 转换为单独的磁盘文件。 Then, I want to be able to read this list of dictionary items and move them to other lists, in different files.然后,我希望能够读取这个字典项目列表并将它们移动到不同文件中的其他列表。

import os

# List 'x' will auto-populate itself with hundreds of new dictionary items upon run - using x.append({'name': f'{var}', 'job': f'{var2}'}) etc.

x = []

i = input('Request: ')

if i == 'add to list':
    with open('example list', 'a') as list:
        list.write(x)
elif i == 'print list':
    with open('example list', 'r') as list:
        list.read(x)
        print(list)

# in this next block, I would like to be able to move an item from the 'example list' list to a different list that is housed in a separate file

elif i == 'move item':
    # 'n' requests an integer value to represent the index in the list
    n = input('Which item do you want to move? ')
    with open('example list', 'r') as list:
        j = list[n]
        # I want to delete 'j' from 'example list' 
    with open('other list', 'a') as other_list:
        # I want to append 'j' to 'other list'
        other_list.write(j)

print('example list')

I am stuck on reading and moving these list items.我一直在阅读和移动这些列表项。 I can't even get it to print "example list" in a nice format.我什至不能让它以很好的格式打印“示例列表”。

I have heard of pickle module, but I've never used it.我听说过pickle模块,但我从未使用过它。 I also understand that it may be necessary to save these lists as json files in order to be able to access the list items and the subsequent dictionary keys within.我也明白可能需要将这些列表保存为 json 文件,以便能够访问列表项和其中的后续字典键。

I think you have a misconception here as list does not convert the content into a list.我认为您在这里有一个误解,因为列表不会将内容转换为列表。 It's like a variable.这就像一个变量。 I have changed some parts of your code.我已经更改了您的代码的某些部分。 I dont know if it would work as you didn't specify any inputs or outputs.我不知道它是否会起作用,因为您没有指定任何输入或输出。 have a look看一看

if i == 'add to list':
    with open('example list', 'a') as f:
        # append whatever there is in 'x' into 'example list'
        f.write(x)
elif i == 'print list':
    with open('example list', 'r') as f:
        file_content=f.read()
        print(file_content)

# in this next block, I would like to be able to move an item from the 'example list' list to a different list that is housed in a separate file

elif i == 'move item':
    # 'n' requests an integer value to represent the index in the list
    n = input('Which item do you want to move? ')
    with open('example list', 'r') as f:
        # .readlines() would return a list of lines from 'example list'
        file_lines = f.readlines()
        # pop line with index 'n'
        j = file_lines.pop(n)
        # I want to delete 'j' from 'example list' 
    with open('other list', 'a') as other_list:
        # I want to append 'j' to 'other list'
        other_list.write(j)

print('example list')

I figured it out.我想到了。 Using the json module I was able to dump the list to an external json file with:使用 json 模块,我能够将列表转储到外部 json 文件:

x = []

with open('json list.json', 'w') as f:
    json.dump(x, f)

I could then load the data back in with:然后我可以使用以下方法重新加载数据:

list1 = open('json list', 'r')
listread = list1.read()
data = json.loads(listread)

For moving items between lists I used this:为了在列表之间移动项目,我使用了这个:

move_item = data[3]
del move_item
json.dumps(data)
move_to = open('other list'. 'a')
move_to.write(move_item)
list1.close()
move_to.close()

Once loaded, the list and subsequent dictionaries within remain intact as their respective objects, thus allowing for access via index or key.加载后,列表和其中的后续字典作为它们各自的对象保持不变,从而允许通过索引或键进行访问。 Thank you to everyone, I used a combo of everyone's suggestions.谢谢大家,我综合了大家的建议。

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

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