简体   繁体   English

在Python中从文本文件中获取* list *内容

[英]Fetching *list* contents from a text file in Python

I need help to fetch list back from a file where each line is stored as a list. 我需要帮助,以便从每行存储为列表的文件中取回列表。 For example, check the following lists.txt file 例如,检查以下lists.txt文件

["1","Andy Allen","Administrator"]
["2","Bob Benny","Moderator"]
["3","Zed Zen","Member"]

I need the content to be accessible and be displayed in the following manner 我需要以以下方式访问和显示内容

SN : 1
Name : Andy Allen
Position : Administrator

SN : 2
Name : Bob Benny
Position : Moderator

SN : 3
Name : Zed Zen
Position : Member

PS : I know I could have saved it with Delimiters and access list elements with split function... But it seems that when using a particular delimiter as the text content causes the function to function abnormally.. PS:我知道我可以使用分隔符保存它,并使用具有拆分功能的访问列表元素保存它……但是,当使用特定的分隔符作为文本内容时,似乎导致该函数无法正常运行。

This format, using only double quotes and not single, looks like JSON (JavaScript object notation) . 这种格式仅使用双引号而不是单引号,类似于JSON(JavaScript对象表示法) Fortunately, the json module that comes with Python 2.6 or later can parse JSON. 幸运的是,Python 2.6或更高版本随附的json模块可以解析JSON。 So read each line, parse it as JSON (using json.loads ), and then print it out. 因此,请阅读每一行,将其解析为JSON(使用json.loads ),然后将其打印出来。 Try something like this: 尝试这样的事情:

import json
with open("lists.txt", "r") as infp:
    rows = [json.loads(line) for line in infp]
for sn, name, position in rows:
    print("SN : %s\nName : %s\nPosition : %s\n" % (sn, name, position))

Taming CSV 驯服CSV

Another solution is to export in tab- or comma-separated values formats and use Python's csv module to read them. 另一种解决方案是导出以制表符或逗号分隔的值格式,并使用Python的csv模块读取它们。 If (for example) a value in a comma-separated file contains a comma, surround it with double quotes ( " ): 如果(例如)逗号分隔文件中的值包含逗号,请用双引号( " )引起来:

2,Bob Benny,"Moderator, Graphic Designer"

And if a value contains double quote characters, double them and surround the whole thing with double quotes. 并且,如果值包含双引号字符,则将它们加倍并用双引号引起来。 For example, the last element of the following row has the value "Fossils" Editor : 例如,下一行的最后一个元素的值为"Fossils" Editor

5,Chester from Tilwick,"""Fossils"" Editor"

A spreadsheet app, such as Excel, LibreOffice Calc, or Gnumeric, will do this escaping for you when saving in separated format. 以分隔格式保存时,电子表格应用程序(例如Excel,LibreOffice Calc或Gnumeric)将为您进行此转义。 Thus lists.csv would look like this: 因此, lists.csv将如下所示:

1,Andy Allen,Administrator
2,Bob Benny,"Moderator, Graphic Designer"
3,Zed Zen,Member
5,Chester from Tilwick,"""Fossils"" Editor"

Which can be parsed similarly: 可以类似地解析:

import csv
with open("lists.csv", "r", newline="") as infp:
    # use "excel" for comma-separated or "excel-tab" for tab-separated
    reader = csv.reader(infp, "excel")
    rows = list(reader)
for sn, name, position in rows:
    print("SN : %s\nName : %s\nPosition : %s\n" % (sn, name, position))

(This is for Python 3. Python 2's csv module differs slightly in that open should be binary: open("lists.txt", "rb") .) (这是针对Python 3的open("lists.txt", "rb")csv模块的不同之处在于open应该是二进制的: open("lists.txt", "rb") 。)

如何使用grep抓取[]中的所有内容?

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

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