首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Python 3中的磁盘读取和编辑列表?

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

Stack Overflow用户
提问于 2020-06-25 18:01:39
回答 2查看 353关注 0票数 0

我有一个字典列表,我想要附加到一个单独的磁盘文件。然后,我希望能够阅读这个字典项目列表,并将它们移到其他列表中,在不同的文件中。

代码语言:javascript
复制
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')

我被困在阅读和移动这些清单项目。我甚至无法让它以一种很好的格式打印“示例列表”。

我听说过pickle模块,但我从未使用过它。我还了解到,可能需要将这些列表保存为json文件,以便能够在其中访问列表项和随后的字典键。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-26 00:00:40

我想通了。使用json模块,我能够将列表转储到外部json文件中:

代码语言:javascript
复制
x = []

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

然后,我可以用以下内容加载数据:

代码语言:javascript
复制
list1 = open('json list', 'r')
listread = list1.read()
data = json.loads(listread)

对于在列表之间移动项目,我使用了以下方法:

代码语言:javascript
复制
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()

加载后,列表和随后的字典作为各自的对象保持不变,从而允许通过索引或键进行访问。谢谢大家,我用了大家的建议。

票数 0
EN

Stack Overflow用户

发布于 2020-06-25 18:19:29

我认为你在这里有一个误解,因为列表不会将内容转换为列表。就像变量一样。我已经更改了你代码的某些部分。我不知道它是否会工作,因为你没有指定任何输入或输出。看一看

代码语言:javascript
复制
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')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62581582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档