2

嘿大家,问题是这样的,老实说,这是家庭作业问题,但我只是被卡住了,它永远存在。我的问题是,由于城市在每一行中都有不同的索引位置,我们如何逐行迭代并将城市名称返回到列表中?(任何可以让我开始的帮助将不胜感激,我并不懒惰,我确实已经为此工作了几个小时)

citylist(filename)  reads a file in the DOT format
      and returns a list of city names, one for each line in 
      the file.  The DOT format may have trailing spaces on 
      the city name;  make sure citylist creates a list of 
      city names with trailing spaces removed (easy using
      Python's strip() method).  Two test cases are shown 
      below, for lines at index 3 and 347.

citylist("DOT500.txt")[3]
  'TUCSON'

citylist("DOT500.txt")[347]
  'NORTH VILLE'

以下是 DOT 文件(其中的一部分)

1   958164  TOYOTA MOTOR CORPORATION    TOYOTA  LAND CRUISER    1994        19941223    N   0   0   SERVICE BRAKES, HYDRAULIC:ANTILOCK  ARNOLD          CA  JT3DJ81W8R0 19950103    19950103            ABS SYSTEM FAILURE, AT 20MPH.  TT   EVOQ                                                                                                    V   

2   958156  TOYOTA MOTOR CORPORATION    TOYOTA  PASEO   1994    Y   19941226    N   0   0   PARKING BRAKE:CONVENTIONAL  SAN JOSE        CA  JT2EL45U5R0 19950103    19950103        1   PARKED ON FLAT SURFACE EMERGENCY BRAKING ENGAGED VEHICLE ROLLED REARWARD.  TT   EVOQ                                                                                                    V   

3   958124  TOYOTA MOTOR CORPORATION    TOYOTA  COROLLA 1994    Y   19941128    N   0   0   AIR BAGS:FRONTAL    PHOENIX         AZ      19950103    19950103            UPON FRONTAL COLLISION, AIR BAG FAILED TO DEPLOY. VEHICLE CLASSIFIED AS TOTALED. PLEASE DESCRIBE DETAILS.  TT   EVOQ                                                                                                    V   

4   958122  NISSAN NORTH AMERICA, INC.  NISSAN  MAXIMA  1994        19950103    N   0   0   SUSPENSION  TUCSON          AZ  JN1HJ01F4RT 19950103    19950103            THE STRUT WAS BAD THERE IS A NOISE ON THE PASSENGER SIDE DOOR AND THE ENGINE LIGHT MALFUNCTION.  TT EVOQ                                                                                                    V   

5   958122  NISSAN NORTH AMERICA, INC.  NISSAN  MAXIMA  1994        19950103    N   0   0   ENGINE AND ENGINE COOLING:ENGINE    TUCSON          AZ  JN1HJ01F4RT 19950103    19950103            THE STRUT WAS BAD THERE IS A NOISE ON THE PASSENGER SIDE DOOR AND THE ENGINE LIGHT MALFUNCTION.  TT EVOQ                                                                                                    V   

6   958247  NISSAN NORTH AMERICA, INC.  NISSAN  PATHFINDER  1994            N   0   0   SUSPENSION:FRONT    ABINGDON        MD      19950103    19950103            FRONT TIRES SHOW EXCESSIVE WEAR AND STEEL BELTS. NISSAN IS OFFERING TO REPLACE TWO TIRES.  TT   EVOQ                                                                                                    V   

7   958143  FORD MOTOR COMPANY  FORD    MUSTANG 1991        19941119    Y   0   0   SEATS:FRONT ASSEMBLY:POWER ADJUST   PENSICOLA       FL      19950103    19950103            DRIVER SIDE POWER SEAT CAUGHT FIRE.  TT EVOQ                                                                                                    V   

8   958141  DAIMLERCHRYSLER CORPORATION CHRYSLER    NEW YORKER  1989        19950103    N   0   0   POWER TRAIN:AUTOMATIC TRANSMISSION  LEVITTOWN       NY  1C3BC4639KD 19950103    19950103        2   TRANSMISSION NOT SHIFTING OUT OF 2ND GEAR, WENT INTO LIM MODE/TRANSMMION NOT SHIFTING THEN JUMPED OUT OF GEAR. *AK  EVOQ                                                                                                    V   

9   958128  DAIMLERCHRYSLER CORPORATION DODGE   SHADOW  1990    Y   19941224    N   0   0   SERVICE BRAKES, HYDRAULIC:ANTILOCK  WILMINGTON      DE  1B3XT44KXLN 19950103    19950103        1   ABS: EXPERIENCED ANTI-LOCK BRAKE FAILURE/EXTENDED STOPPING DISTANCE, RESULTING IN AN ACCIDENT, IMPACT 12:00 POSITION SPEED, 20 MPH.  TT EVOQ                                                                                                    V   

10  958128  DAIMLERCHRYSLER CORPORATION DODGE   SHADOW  1990    Y   19940412    N   0   0   SUSPENSION:FRONT    WILMINGTON      DE  1B3XT44KXLN 19950103    19950103        1   MISALIGNMENT, CAUSING VEHICLE TO VEER TO THE CENTER.  *AK   EVOQ                                                                                                    V   

11  958128  DAIMLERCHRYSLER CORPORATION DODGE   SHADOW  1990    Y   19941117    N   0   0   ENGINE AND ENGINE COOLING:ENGINE    WILMINGTON      DE  1B3XT44KXLN 19950103    19950103        1   ENGINE MOTOR MOUNTS FAILED, RESULTING IN ENGINE NOISE.  *AK EVOQ                                                                                                    V   

12  958168  GENERAL MOTORS CORP.    CHEVROLET   SUBURBAN    1995    Y   19950101    N   0   0   POWER TRAIN:AUTOMATIC TRANSMISSION:GEAR POSITION INDICATION (PRNDL) SHAFTFBURG      MI      19950103    19950103            VEHICLE ROLLED DOWN HILL WHILE IN PARK, AND INTO A TREE.  TT    EVOQ                                                                                                    V   

13  958169  GENERAL MOTORS CORP.    GEO METRO   1995    Y   19941225    N   1   0   SERVICE BRAKES, HYDRAULIC:FOUNDATION COMPONENTS NORFOLK         VA      19950103    19950103            BRAKES WOULD NOT WORK ON SEVERAL OCCASIONS, LAST INSTANCE RESULTED IN AN ACCIDENT.  TT  EVOQ    
4

1 回答 1

4

在一般情况下,这将非常非常困难。但我们可以看看他们是否让我们变得更容易。我们可以读一行看一下:

In [31]: first_line = open("toyota.dot").readline()

In [32]: print repr(first_line)
'    1\t958164\tTOYOTA MOTOR CORPORATION\tTOYOTA\tLAND CRUISER\t1994\t\t19941223\tN\t0\t0\tSERVICE BRAKES, HYDRAULIC:ANTILOCK\tARNOLD      \tCA\tJT3DJ81W8R0\t19950103\t19950103\t\t\tABS SYSTEM FAILURE, AT 20MPH.  TT\tEVOQ\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tV\t\n'

看到所有这些\t符号了吗?我们很幸运——这些列由制表符分隔!所以一种方法是使用.split()行上的方法将它们分成列:

In [33]: first_line.split("\t")
Out[33]: 
['    1',
 '958164',
 'TOYOTA MOTOR CORPORATION',
 'TOYOTA',
 'LAND CRUISER',
 '1994',
[... etc ...]
 '\n']

然后我们可以提取列:

In [34]: first_line.split("\t")[11]
Out[34]: 'SERVICE BRAKES, HYDRAULIC:ANTILOCK'

In [35]: first_line.split("\t")[12]
Out[35]: 'ARNOLD      '

并摆脱空间。

In [36]: first_line.split("\t")[12].strip()
Out[36]: 'ARNOLD'

您可以使用

with open("toyota.dot") as data:
    for line in data:
        parts = line.split("\t")

模式来迭代线条,其余的你应该能够拼凑起来。

[旁白:我实际上会使用csv.readerwith delimiter="\t",但这可能有点超出老师的要求。]

于 2012-10-28T18:54:20.130 回答