1

I'm new to Python and I'm learning from a tutorial, and there is this line here that is suppose to read from a .csv file and skip the first line (header):

data = open("banklist.csv", "r").readlines()[1:]
for entry in data:
  #do some stuffs

Problem is, it is not even entering the loop. Removing the [1:] in the open statement and adding print data, I find that the csv seems to be read as one big line, and removing the header removed everything instead:

["First National Bank also operating as The National Bank of El Paso,Edinburg,TX,14318,13-Sep-13,18-Sep-13\rThe Community's Bank,Bridgeport,CT,57041,13-Sep-13,17-Sep-13\rSunrise Bank of Arizona,Phoenix,AZ,34707,23-Aug-13,11-Sep-13\rCommunity South Bank,Parsons,TN,19849,23-Aug-13,5-Sep-13\rBank of Wausau,Wausau,WI,35016,9-Aug-13,4-Sep-13\rFirst Community Bank of Southwest Florida (also operating as Community Bank of Cape Coral),Fort Myers,FL,34943,2-Aug-13,26-Aug-13\rMountain National Bank,Sevierville,TN,34789,7-Jun-13,26-Aug-13\r1st Commerce Bank,North Las Vegas,NV,58358,6-Jun-13,26-Aug-13\rBanks of Wisconsin d/b/a Bank of Kenosha,Kenosha,WI,35386,31-May-13,12-Jul-13\rCentral Arizona Bank,Scottsdale,AZ,34527,14-May-13,26-Aug-13\rSunrise Bank,Valdosta,GA,58185,10-May-13,12-Jul-13\rPisgah Community Bank,Asheville,NC,58701,10-May-13,26-Aug-13\rDouglas County Bank,Douglasville,GA,21649,26-Apr-13,26-Aug-13\rParkway Bank,Lenoir,NC,57158,26-Apr-13,26-Aug-13\rChipola Community Bank,Marianna,FL,58034,19-Apr-13,12-Jul-13\rHeritage Bank of North Florida,Orange Park,FL,26680,19-Apr-13,26-Aug-13\rFirst Federal Bank,Lexington,KY,29594,19-Apr-13,12-Jul-13"]

How do I go about this?

10
  • 1
    Python has a csv parser module Commented Oct 4, 2013 at 8:58
  • I could use that module, but any idea why is this happening? Commented Oct 4, 2013 at 9:00
  • Maybe a mismatch of newline escape sequences? Commented Oct 4, 2013 at 9:03
  • 1
    The carriage returns \r are not being handled correctly hence the error, use the csv module Commented Oct 4, 2013 at 9:03
  • 1
    I must say I don't quite see why this question was downvoted; the OP showed what he tried, what output was expected, what he got instead. The naive comment of 'use the csv module' doesn't actually solve the problem either. Commented Oct 4, 2013 at 9:19

1 Answer 1

1

You are reading a text file that uses a different newline convention from your system; \r where probably your system expects either \n or \r\n.

You can open the file using universal newline support instead with the 'rU' mode:

data = open("banklist.csv", "rU").readlines()[1:]

The smarter way to skip that first line is to use the file as an iterator; loop over it directly. The next() method lets you grab one line at a time, giving you a method to skip the first line:

with open("banklist.csv", "rU") as infile:
    next(infile, None)  # skip the first line
    for line in infile:
        print line 

However, if this a CSV file, use the csv module to read the data:

import csv

with open("banklist.csv", "rU") as infile:
    reader = csv.reader(infile)
    next(reader, None)  # skip the first row
    for row in reader:
        print row

The CSV module handles splitting the lines into lists for you:

['First National Bank also operating as The National Bank of El Paso', 'Edinburg', 'TX', '14318', '13-Sep-13', '18-Sep-13']
["The Community's Bank", 'Bridgeport', 'CT', '57041', '13-Sep-13', '17-Sep-13']
['Sunrise Bank of Arizona', 'Phoenix', 'AZ', '34707', '23-Aug-13', '11-Sep-13']
['Community South Bank', 'Parsons', 'TN', '19849', '23-Aug-13', '5-Sep-13']
['Bank of Wausau', 'Wausau', 'WI', '35016', '9-Aug-13', '4-Sep-13']
['First Community Bank of Southwest Florida (also operating as Community Bank of Cape Coral)', 'Fort Myers', 'FL', '34943', '2-Aug-13', '26-Aug-13']
# etc.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That \r looked really suspicious.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.