#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    How do I open a csv file?


    Hi all,

    All I want to do is open a csv file in Python. So my code is simply:

    Code:
    myfile = open('C:\\Users\Me\test.csv','r')
    It then complains with:

    'import sitecustomize' failed; use -v for traceback Traceback (most recent call last): File "C:\Users\Me\Scratch.py", line 15, in <module> myfile = open('C:\\Users\Me\test.csv','r') IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\\Me\test.csv'
    If I copy the data from the csv file to a txt file it works.

    I have no idea what's going on really. I've tried looking up about csv files but all I can find is information about a ready-built csv module which I don't want to use.

    I appreciate any help!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    I think your trouble involves strings.
    Try this:
    Code:
    myfile = open(r'C:\\Users\Me\test.csv','r')
    Why might this work?
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    myfile = open('C:\\Users\Me\test.csv','r')
    Note that the \t in your string is interpreted as a tab character.
    Use:
    myfile = open('C:\\Users\\Me\\test.csv','r')
    Or better (Windows allows this):
    myfile = open('C:/Users/Me/test.csv','r')

    I would stay out the Users folder!
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0
    if you are using windows you need to use double \
    Code:
    myfile = open('C:\\Users\\Me\\test.csv','r')
    this should work

IMN logo majestic logo threadwatch logo seochat tools logo