从列表中组织来自csv文件的数据

时间:2018-04-15 02:17:21

标签: python list loops csv filter

所以我需要帮助我的代码来获取这个

Column Sum
Company 27
Booth 27
Full-Time 27
Full-Time Visa Sponsor 27
Part-Time 27
Internship 27
Freshman 27
Sophomore 27
Junior 27
Senior 27
Post-Bacs 27
MS 27
PhD 27
Alumni 27

但是我的代码现在输出了这个

company_dict = {0:"Company", 1:"Booth",
                2:"Full-Time", 3:"Full-Time Visa Sponsor",
                4:"Part-Time", 5:"Internship",
                6:"Freshman", 7:"Sophomore",
                8:"Junior", 9:"Senior",
                10:"Post-Bacs", 11:"MS",
                12:"PhD", 13:"Alumni"}

                                            #Loop to organize the company_dict
for lines in company_dict:
    print(repr(lines),company_dict[lines])

keywords = ("AIG","Baylor","CGG","Citi","ExxonMobil","Flow-Cal Inc.",                   #I used a list to help me get the information I wanted from the csv file
           "Global SHop Solutions","Harris Count CTS","HCSS",
           "Hitachi Consulting", "HP Inc.","INT Inc.","JPMorgan Chase & Co",
           "Leidos","McKesson","MRE Consulting Ltd.","NetIQ","PROS",
           "San Jacinto College","SAS","Smartbridge","Sogeti USA",
           "Southwest Research Institute","The Reynolds and Reynolds Company",
           "UH Enterprise Systems","U.S. Marine Corps","ValuD Consuting LLC","Wipro")

DataList = []                                                                           #I made a blank list
with f as filterf:                                                                      #This loop will look for the keywords in the file, and only add those keywords
    output_line_counter = 0                                                             #I needed it to print with rows, so I set it to 0
    for line in filterf:
        if any(keyword in line for keyword in keywords):                                #The actual code that looks for keywords in the line in my file
            output_line_counter += 1                                                    #Adds the column (might not be necessary but it works for me)
            DataList.append(line)

CleanerData = sorted(set(DataList))                                                     #I made a new 'cleaner' list so that it would be alphabetically without spaces
line_counter = 0
for i in CleanerData:                                                                   #I had to do another loop to add rows again, it now prints what is required in the question
    line_counter += 1
    print(line_counter, i, end='')

data_employer = {'No': ('Column', 'Sum')}
for empdata in range(14):
    sum = 0
    for i in CleanerData:
        if i[empdata] != '':
            sum += 1
    data_employer[empdata] = (company_dict[empdata], sum)
for k in data_employer:
    print(list(data_employer.keys()).index(k), data_employer[k][0], data_employer[k][1])

我不得不使用来自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,,

我真的没有得到27来自哪里,我猜它是因为我没有看到的一些逻辑错误。这是我对代码的尝试,任何输入都将不胜感激。

谢谢!

原始CSV文件

    <dependency>
        <groupId>com.outworkers</groupId>
        <artifactId>phantom-dsl_2.11</artifactId>
        <version>2.24.1</version>
    </dependency>

更新:我已经放置了更多代码以帮助澄清。仍在弄清楚为什么它只打印27个列表。我不能在这个项目中使用pandas

2 个答案:

答案 0 :(得分:0)

替换最后一行candleDiv.data[0].open[candleDiv.data[0].open.length - 1] = updatedOpenValue; candleDiv.data[0].close[candleDiv.data[0].close.length - 1] = updatedCloseValue; candleDiv.data[0].high[candleDiv.data[0].high.length - 1] = updatedHighValue; candleDiv.data[0].low[candleDiv.data[0].low.length - 1] = updatedLowValue; Plotly.restyle(candleDiv, 'data[0]', candleDiv.data[0], [0]);

print(data_employer[k][0], data_employer[k][1])

答案 1 :(得分:0)

这是一个使用pandas的简单解决方案

import pandas as pd

csv_file_in = 'lines.csv'
csv_file_out = 'return.csv'

df = pd.read_csv(csv_file_in, header=2) # Read in CSV header=2 makes the headers ALumni, PhD etc etc

headers = list(df.columns.values) # get a list of columns to count (headers as row 2)

temp_df = pd.DataFrame([]) # create temp df

for i in headers: #iterate through the columns
    try:
        new_df = pd.DataFrame({'Sum': df[i].count().sum()}, index=[i]) # new dataframe as holding (will be overwritten in the next iteration)
        temp_df = pd.concat([new_df, temp_df]) # concat to temp_df
    except KeyError as e:
        print(e)


temp_df.to_csv(csv_file_out) #output to csv
print(temp_df)

输出

                    Sum
Alumni                   15
PhD                       6
MS                       17
Post-Bacs                17
Senior                   24
Junior                   17
Sophomore                 9
Freshman                  7
Internship               18
Part-Time                 1
Full-Time Visa Sponsor    5
Full-Time                25
Booth                    28
Company                  29