尝试追加,从csv文件中放入数据

时间:2018-04-17 07:49:59

标签: python list csv

我正在尝试从我的csv文件中的某些行中获取数据。如果它在行中有一定的值('YES'),它将与我制作的词典相符。我应该得到这个

的输出
No Column Sum
0 Company 28
1 Booth 28
2 Full-Time 25
3 Full-Time Visa Sponsor 5
4 Part-Time 1
5 Internship 18
6 Freshman 7
7 Sophomore 9
8 Junior 17
9 Senior 24
10 Post-Bacs 17
11 MS 17
12 PhD 6
13 Alumni 15

但是现在我得到了这个

{'No': ('Column', 'Sum')}

使用此代码

import csv
filename = ("CFSpring2018Employers.csv")                       #I assigned the file to a variable
f = open(filename)                        #I couldn't leave it default due to UTF-8 error from orginial
reader = csv.reader(f)
f.close

#Dictionary for part 1 'Companies'
companies = {}
countComp = 0 #Sets the 'rows to 0

for row in reader: #reader has assigned value to read csv info
    if row[0] == 'Company': #Searches row called 'Company'
        for item in row:
            companies[countComp] = item #Everytime it shows it puts it into the dictionary
            countComp += 1
        break

for i in companies: #Allows me to print numbers next to the companies dict
    print(i, companies[i])

ReadyColumn = []    #Making a new list for cleaned up data
NumCow = 0
for row in reader:
    NumCow +=1
    if NumCow < 31 and row[0] != '': #This will read the rows between 0-31, and if the row contains 'Nothing' it skips
        ReadyColumn.append(row) #Updates the list
rowNum = -1 #Resets the counter so that AIG starts at 0

for row in ReadyColumn:
    rowNum += 1
    print(rowNum, ','.join(row)) #Joins the rows with each value separated by ,

data_employer = {'No': ('Column', 'Sum')}

for values in range(14):    #Their is 14 rows that need to be counted up
    sum = 0
    for row in ReadyColumn:
        if row[values] != '':
            sum += 1
        cleaner_employer = (companies[values], sum)
for i in data_employer:
    cleaner_employer = (i, data_employer[i][0], data_employer[i][1])

out_employer = []

for i in data_employer:
    row = []
    row.append(i)
    row.append(data_employer[i][0])
    row.append(data_employer[i][1])
    out_employer.append(row)
print(data_employer)

这是我正在使用的csv文件信息

ALPHABETICAL ORDER,,,,,,,,,,,,,
,,Positions,,,,Classifications,,,,,,,
Company,Booth,Full-Time,"Full-Time Visa Sponsor",Part-Time,Internship,Freshman,Sophomore,Junior,Senior,Post-Bacs,MS,PhD,Alumni
AIG,10,,,,Yes,,,Jr,,,MS,,
Baylor�College�of�Medicine,19,Yes,Yes,,,,,,,,,,Recent
CGG,17,Yes,Yes,,,,,,,,MS,PhD,Recent
Citi,27/28,Yes,,,Yes,,,Jr,Sr,,,,
ExxonMobil,11,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,,,
,...
Flow-Cal�Inc.,16,Yes,,,Yes,,,Jr,Sr,,,,All
Global�Shop�Solutions,18,Yes,,,Yes,,,,Sr,PB,,,All
Harris�County�CTS,22,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
HCSS,29,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
Hitachi�Consulting,13,Yes,,,,,,,Sr,,MS,,
HP�Inc.,1,Yes,,,Yes,,,Jr,,,MS,,Recent
INT�Inc.,20,Yes,Yes,,Yes,,,Jr,Sr,,MS,PhD,
JPMorgan�Chase�&�Co,3,Yes,,,Yes,,,Jr,Sr,,,,
Leidos,390,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,
McKesson,26,Yes,,,,,,,Sr,,,,
,,,,,,,,,,,,,
MRE�Consulting�Ltd.,2,Yes,,,,,,,Sr,PB,MS,,All
NetIQ,7,,,,Yes,,Soph,Jr,Sr,PB,,,
PROS,21,Yes,,,,,,,Sr,,MS,PhD,All
San�Jacinto�College��,14,,,,Yes,,Soph,Jr,Sr,PB,MS,,
SAS,4,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
Smartbridge,8,Yes,,,,,,,Sr,PB,MS,,
Sogeti�USA,15,Yes,,,,,,,Sr,PB,MS,,
Southwest�Research�Institute,12,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
The�Reynolds�and�Reynolds�Company,23,Yes,Yes,,Yes,Fr,Soph,Jr,Sr,PB,,,All
UH�Enterprise�Systems,9,Yes,Yes,Yes,Yes,Fr,Soph,Jr,Sr,PB,MS,PhD,All
U.S.�Marine�Corps,25,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,All
ValuD�Consuting�LLC,5,Yes,,,,,,,Sr,PB,,,All
Wipro,24,Yes,,,,,,,Sr,PB,,,
BOOTH ORDER,,,,,,,,,,,,,
,Booth,Positions,,,,Classifications,,,,,,,
Company,#,Full-Time,"Full-Time
Visa Sponsor",Part-Time,Internship,Freshman,Sophomore,Junior,Senior,Post-Bacs,MS,PhD,Alumni
HP�Inc.,1,Yes,,,Yes,,,Jr,,,MS,,Recent
"MRE�Consulting,�Ltd.",2,Yes,,,,,,,Sr,PB,MS,,All
JPMorgan�Chase�&�Co,3,Yes,,,Yes,,,Jr,Sr,,,,
SAS,4,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
ValuD�Consuting�LLC,5,Yes,,,,,,,Sr,PB,,,All
NetIQ,7,,,,Yes,,Soph,Jr,Sr,PB,,,
Smartbridge,8,Yes,,,,,,,Sr,PB,MS,,
UH�Enterprise�Systems,9,Yes,Yes,Yes,Yes,Fr,Soph,Jr,Sr,PB,MS,PhD,All
AIG,10,,,,Yes,,,Jr,,,MS,,
ExxonMobil,11,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,,,
Southwest�Research�Institute,12,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
Hitachi�Consulting,13,Yes,,,,,,,Sr,,MS,,
San�Jacinto�College��,14,,,,Yes,,Soph,Jr,Sr,PB,MS,,
Sogeti�USA,15,Yes,,,,,,,Sr,PB,MS,,
"Flow-Cal,�Inc.",16,Yes,,,Yes,,,Jr,Sr,,,,All
CGG,17,Yes,Yes,,,,,,,,MS,PhD,Recent
Global�Shop�Solutions,18,Yes,,,Yes,,,,Sr,PB,,,All
Baylor�College�of�Medicine,19,Yes,Yes,,,,,,,,,,Recent
"INT,�Inc.",20,Yes,Yes,,Yes,,,Jr,Sr,,MS,PhD,
PROS,21,Yes,,,,,,,Sr,,MS,PhD,All
Harris�County�CTS,22,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
The�Reynolds�and�Reynolds�Company,23,Yes,Yes,,Yes,Fr,Soph,Jr,Sr,PB,,,All
Wipro,24,Yes,,,,,,,Sr,PB,,,
U.S.�Marine�Corps,25,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,All
McKesson,26,Yes,,,,,,,Sr,,,,
Citi,27/28,Yes,,,Yes,,,Jr,Sr,,,,
HCSS,29,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
Leidos,30,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,

我只从某些行获取数据而不是整个事物(因此我只能从0到30获得)

['No', 'Column', 'Sum']
['No', 'Column', 'Sum', 0, 'Company', 28]
['No', 'Column', 'Sum', 0, 'Company', 28, 1, 'Booth', 28]
['No', 'Column', 'Sum', 0, 'Company', 28, 1, 'Booth', 28, 2, 'Full-Time', 25]
['No', 'Column', 'Sum', 0, 'Company', 28, 1, 'Booth', 28, 2, 'Full-Time', 25, 3, 'Full-Time Visa Sponsor', 5]
['No', 'Column', 'Sum', 0, 'Company', 28, 1, 'Booth', 28, 2, 'Full-Time', 25, 3, 'Full-Time Visa Sponsor', 5, 4, 'Part-Time', 1]
['No', 'Column', 'Sum', 0, 'Company', 28, 1, 'Booth', 28, 2, 'Full-Time', 25, 3, 'Full-Time Visa Sponsor', 5, 4, 'Part-Time', 1, 5, 'Internship', 18]
['No', 'Column', 'Sum', 0, 'Company', 28, 1, 'Booth', 28, 2, 'Full-Time', 25, 3, 'Full-Time Visa Sponsor', 5, 4, 'Part-Time', 1, 5, 'Internship', 18, 6, 'Freshman', 7]
['No', 'Column', 'Sum', 0, 'Company', 28, 1, 'Booth', 28, 2, 'Full-Time', 25, 3, 'Full-Time Visa Sponsor', 5, 4, 'Part-Time', 1, 5, 'Internship', 18, 6, 'Freshman', 7, 7, 'Sophomore', 9]
['No', 'Column', 'Sum', 0, 'Company', 28, 1, 'Booth', 28, 2, 'Full-Time', 25, 3, 'Full-Time Visa Sponsor', 5, 4, 'Part-Time', 1, 5, 'Internship', 18, 6, 'Freshman', 7, 7, 'Sophomore', 9, 8, 'Junior', 17]
['No', 'Column', 'Sum', 0, 'Company', 28, 1, 'Booth', 28, 2, 'Full-Time', 25, 3, 'Full-Time Visa Sponsor', 5, 4, 'Part-Time', 1, 5, 'Internship', 18, 6, 'Freshman', 7, 7, 'Sophomore', 9, 8, 'Junior', 17, 9, 'Senior', 24]
['No', 'Column', 'Sum', 0, 'Company', 28, 1, 'Booth', 28, 2, 'Full-Time', 25, 3, 'Full-Time Visa Sponsor', 5, 4, 'Part-Time', 1, 5, 'Internship', 18, 6, 'Freshman', 7, 7, 'Sophomore', 9, 8, 'Junior', 17, 9, 'Senior', 24, 10, 'Post-Bacs', 17]
['No', 'Column', 'Sum', 0, 'Company', 28, 1, 'Booth', 28, 2, 'Full-Time', 25, 3, 'Full-Time Visa Sponsor', 5, 4, 'Part-Time', 1, 5, 'Internship', 18, 6, 'Freshman', 7, 7, 'Sophomore', 9, 8, 'Junior', 17, 9, 'Senior', 24, 10, 'Post-Bacs', 17, 11, 'MS', 17]
['No', 'Column', 'Sum', 0, 'Company', 28, 1, 'Booth', 28, 2, 'Full-Time', 25, 3, 'Full-Time Visa Sponsor', 5, 4, 'Part-Time', 1, 5, 'Internship', 18, 6, 'Freshman', 7, 7, 'Sophomore', 9, 8, 'Junior', 17, 9, 'Senior', 24, 10, 'Post-Bacs', 17, 11, 'MS', 17, 12, 'PhD', 6]
['No', 'Column', 'Sum', 0, 'Company', 28, 1, 'Booth', 28, 2, 'Full-Time', 25, 3, 'Full-Time Visa Sponsor', 5, 4, 'Part-Time', 1, 5, 'Internship', 18, 6, 'Freshman', 7, 7, 'Sophomore', 9, 8, 'Junior', 17, 9, 'Senior', 24, 10, 'Post-Bacs', 17, 11, 'MS', 17, 12, 'PhD', 6, 13, 'Alumni', 15]

不能使用pandas模块。不允许作业,如果我能理解这一部分,我将能够完成下一部分。因为我是python的新手,所以给了很多困难。谢谢

1 个答案:

答案 0 :(得分:1)

你是如此亲密。你只需改变一点:

for values in range(14):    # Their are 14 rows that need to be counted up
    sum = 0
    for row in ReadyColumn:
        if row[values] != '':
            sum += 1
    cleaner_employer = (companies[values], sum)
    data_employer[values] = cleaner_employer