从文件中分隔的逗号项创建列表

create a list from separated comma items in a file

您好,我有以下脚本,但是这个脚本正在以这种方式检索文件中的项目

ca1
ca2
ca3

我的新文本文件是这样排列的

ca1, ca2, ca3

这是我的脚本,我应该修改什么才能做到这一点?

with open('fileids2.txt', 'r') as f:
genres=[line.strip() for line in f]  

freq = nltk.ConditionalFreqDist(
 (genre, m)
  for genre in brown.fileids()
  for m in brown.words(fileids=genre))

adj = ["new", "such", "own","good",]
freq.tabulate(conditions=genres, samples=adj)

您可以使用 csv 模块。

from csv import reader
with open('fileids2.txt', 'r') as f:
     words= reader(f, delimiter=',', quotechar='|')
     for word in words:
         print ','.join(word)

这将输出文件中的行:

ca1, ca2, ca3

上述代码也适用于多行文件。

您可以阅读有关 csv 模块的更多信息 here

如果您的数据文件非常简单,您可以只 split 该行。

with open('fileids2.txt', 'r') as f:
    genres = [word.strip() for word in f.next().split(',')]

如果数据项中可能有逗号(例如 ca1, "ca, 2", ca3),那么您应该使用 csv 模块来正确解析它,如@JoãoGFarias 的回答。