rscronic
rscronic

Reputation: 11

How to substitute string.split() used in Python 2 to Python 3 code?

When I run the following code (from chapter 19 of Introduction to Computation and Programming Using Python by John Guttag):

dataFile = open(fName, 'r')
    for line in dataFile:
        dataLine = string.split(line[:-1], ',')

I receive the following error:

error message:module 'string' has no attribute 'split'

Upvotes: 1

Views: 2397

Answers (2)

Billy Ferguson
Billy Ferguson

Reputation: 1439

You don't define the string anywhere. It shouldn't work on python 2 or 3. You might want two ::, but that's for you to find out.

dataFile = open(fName, 'r')
    for line in dataFile:
        dataLine = line[:-1].split(',')

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 117946

Change this

dataLine = string.split(line[:-1], ',')

to this

dataLine = line[:-1].split(',')

Upvotes: 1

Related Questions