使用csv模块从python中的某些行读取

时间:2018-04-16 08:46:45

标签: python csv

所以我从python中的csv文件获取此信息,我使用的是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 AIG,10,,,,Yes,,,Jr,,,MS,,
1 Baylor College of Medicine,19,Yes,Yes,,,,,,,,,,Recent
2 CGG,17,Yes,Yes,,,,,,,,MS,PhD,Recent
3 Citi,27/28,Yes,,,Yes,,,Jr,Sr,,,,
4 ExxonMobil,11,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,,,
5 Flow-Cal Inc.,16,Yes,,,Yes,,,Jr,Sr,,,,All
6 Global Shop Solutions,18,Yes,,,Yes,,,,Sr,PB,,,All
7 Harris County CTS,22,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
8 HCSS,29,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
9 Hitachi Consulting,13,Yes,,,,,,,Sr,,MS,,
10 HP Inc.,1,Yes,,,Yes,,,Jr,,,MS,,Recent
11 INT Inc.,20,Yes,Yes,,Yes,,,Jr,Sr,,MS,PhD,
12 JPMorgan Chase & Co,3,Yes,,,Yes,,,Jr,Sr,,,,
13 Leidos,390,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,
14 McKesson,26,Yes,,,,,,,Sr,,,,
15 MRE Consulting Ltd.,2,Yes,,,,,,,Sr,PB,MS,,All
16 NetIQ,7,,,,Yes,,Soph,Jr,Sr,PB,,,
17 PROS,21,Yes,,,,,,,Sr,,MS,PhD,All
18 San Jacinto College ,14,,,,Yes,,Soph,Jr,Sr,PB,MS,,
19 SAS,4,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
20 Smartbridge,8,Yes,,,,,,,Sr,PB,MS,,
21 Sogeti USA,15,Yes,,,,,,,Sr,PB,MS,,
22 Southwest Research Institute,12,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
23 The Reynolds and Reynolds Company,23,Yes,Yes,,Yes,Fr,Soph,Jr,Sr,PB,,,All
24 UH Enterprise Systems,9,Yes,Yes,Yes,Yes,Fr,Soph,Jr,Sr,PB,MS,PhD,All
25 U.S. Marine Corps,25,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,All
26 ValuD Consuting LLC,5,Yes,,,

所以代码应该跳过任何有" ,,,"的行。并且只给我一些我需要的行(0-26)。

我试过四处寻找,我看到了很多pandas模块的帮助,但是我不能使用pandas模块进行这项任务。

我试过这样做

with open('Spring.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter='\t')
    next(csv_reader)
    next(csv_reader)
    next(csv_reader)

    for line in csv_reader:
        print(line)

然而,我看到这并不是有效的,我相信它们是一种更好的数据排序方法。任何意见或建议将不胜感激:)

1 个答案:

答案 0 :(得分:0)

这是你想要做的。您的问题是您的打印件甚至打印非ascii字符。我已添加re.sub以仅获取ascii字符。

import csv
import re
repeatCheck = []
with open('Spring.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=' ')
    idx = 1
    for line in list(csv_reader)[3:]:
            rowdata = ",".join(i for i in line)

            if (not rowdata.startswith(",,,")) and (rowdata not in repeatCheck) and (rowdata != ""):
                print(idx, re.sub('[^a-zA-Z0-9\n\.,]', ' ', rowdata ))
                idx+=1
                repeatCheck.append(rowdata)

<强>输出:

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