我已经获得了一个CSV二进制文件,如下例所示:
Apple Orange Tea Coffee Cookie Group
1 0 1 0 1 A
0 1 1 0 1 B
1 0 1 0 0 A
1 1 1 0 0 A
0 1 1 0 1 B
我想把这个表格变成一个句子格式(逐句,只记录二进制看起来是1的项目):
Apple Tea Cookie A
Orange Tea Cookie B
Apple Tea A
Apple Orange Tea A
Orange Tea Cookie B
答案 0 :(得分:1)
你会在Python中使用这些行(假设文件是以制表符分隔的):
import csv
with open('file_name.csv', 'r') as flh:
csvr = csv.reader(flh, delimiter='\t')
result = []
first = None
for row in csvr:
if first is None:
first = row
else:
sent = []
for i, el in enumerate(row[:-1]):
if el.strip() == '1':
sent.append(first[i])
sent.append(row[-1].strip())
result.append(' '.join(sent))
print (result)
答案 1 :(得分:1)
在python中使用csv和DictReader:
import csv
with open('your_file') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
toprint = [key for key in row.keys() if row[key] == 1]
toprint.append(row['Group'])
print(toprint)
<强> outout:强>
['apple', 'Tea', 'Cookie', 'A']
['Tea', 'Cookie', 'Orange', 'B']
['apple', 'Tea', 'A']
['apple', 'Tea', 'Orange', 'A']
['Tea', 'Cookie', 'Orange', 'B']