我有这个简单的python程序:
import csv
f = open('comp.csv')
csv_f = csv.reader(f)
for row in csv_f:
string = row[2]
array = string.split(":")
for word in array:
print(word)
f.close()
我的数据如下:
,Organization,Score 1,Score 2
,Snap Election,"Overall evaluation: 3
Invite to interview: 3
Strength or novelty of the idea (1): 4
Strength or novelty of the idea (2): 4
Strength or novelty of the idea (3): 3
Use or provision of open data (1): 4
Use or provision of open data (2): 4
""Open by default"" (1): 4
""Open by default"" (2): 3
Value proposition and potential scale (1): 4
Value proposition and potential scale (2): 3
Market opportunity and timing (1): 4
Market opportunity and timing (2): 4
Triple bottom line impact (1): 3
Triple bottom line impact (2): 4
Triple bottom line impact (3): 3
Knowledge and skills of the team (1): 4
Knowledge and skills of the team (2): 3
Capacity to realise the idea (1): 2
Capacity to realise the idea (2): 1
Capacity to realise the idea (3): 4
Appropriateness of the budget to realise the idea: 4","Overall evaluation: 3
Invite to interview: 3
Strength or novelty of the idea (1): 4
Strength or novelty of the idea (2): 4
Strength or novelty of the idea (3): 3
Use or provision of open data (1): 3
Use or provision of open data (2): 4
""Open by default"" (1): 3
""Open by default"" (2): 2
Value proposition and potential scale (1): 3
Value proposition and potential scale (2): 4
Market opportunity and timing (1): 3
Market opportunity and timing (2): 2
Triple bottom line impact (1): 3
Triple bottom line impact (2): 3
Triple bottom line impact (3): 2
Knowledge and skills of the team (1): 4
Knowledge and skills of the team (2): 3
Capacity to realise the idea (1): 2
Capacity to realise the idea (2): 2
Capacity to realise the idea (3): 3
Appropriateness of the budget to realise the idea: 3"
,Rental Hotspots Ltd,"Overall evaluation: 1
Invite to interview: 1
我想要做的是在分隔符:
之后抓取数字,它们将始终为1-4。
理想输出类似于:
Snap Election,3,4,3,4,5,4,3,2,1,2,3,4...
等,对于数据中的每个组织。
怎么做到的?
答案 0 :(得分:1)
试试left, sep, right = str.partition(":")
然后,如果不是right
,则可以打印None
。
答案 1 :(得分:1)
以下内容可以满足您的需求:
import csv
with open('comp.csv', 'r') as f_input:
csv_input = csv.reader(f_input)
header = next(csv_input)
for row in csv_input:
counts = [line.split(':')[1].strip() for line in row[2].splitlines()]
print "{},{}".format(row[1], ",".join(counts))
显示以下内容:
Snap Election,3,3,4,4,3,4,4,4,3,4,3,4,4,3,4,3,4,3,2,1,4,4
Rental Hotspots Ltd,1,1
答案 2 :(得分:1)
python中的split函数会将分隔符分隔成数组。 所以,每行你将得到2个成员的数组 array [0] ='text before:' array [1] ='text after:'
你需要的就是删除数组中的单词:line并将print(word)更改为print(array [1])并更改一些错误行
import csv
f = open('comp.csv')
csv_f = csv.reader(f)
for row in csv_f:
array = row.split(":")
print(array[1] + ",")
f.close()
答案 3 :(得分:1)
ENSMUSG00000000001:001
ENSMUSG00000000001:002
ENSMUSG00000000001:003
ENSMUSG00000000002:003
ENSMUSG00000000002:003
ENSMUSG00000000003:002
返回:
ENSMUSG00000000001 6
ENSMUSG00000000002 6
ENSMUSG00000000003 2
awk -F':' -v OFS='\t' '{x=$1;$1="";a[x]=a[x]$0}END{for(x in a)print x,a[x]}' file > output.txt