0
$\begingroup$

I've a dat file in the following format which I'm trying to load using pandas.

28::Persuasion (1995)::Romance
29::City of Lost Children, The (1995)::Adventure|Sci-Fi
30::Shanghai Triad (Yao a yao yao dao waipo qiao) (1995)::Drama
31::Dangerous Minds (1995)::Drama
32::Twelve Monkeys (1995)::Drama|Sci-Fi
33::Wings of Courage (1995)::Adventure|Romance
34::Babe (1995)::Children's|Comedy|Drama

Code

movies = pd.read_csv('movies.dat')

I'm getting this error

ParserError: Error tokenizing data. C error: Expected 1 fields in line 11, saw 2

Line 11:

11::American President, The (1995)::Comedy|Drama|Romance

I understand that it's because of the comma. One option I found is to skip these lines, which I don't want to. Is it possible to set newline as the delimiter here? Any help will be truly appreciated.

$\endgroup$
3
  • $\begingroup$ What is the command you are using? $\endgroup$ – David Masip May 6 '18 at 7:46
  • $\begingroup$ Do you want to separate by | or by ::? $\endgroup$ – David Masip May 6 '18 at 7:55
  • $\begingroup$ @DavidMasip :: should be fine. later i can sort out the genre which are separated by | $\endgroup$ – Van Peer May 6 '18 at 7:57
2
$\begingroup$

You have to tell

pd.read_csv 

the separator that you want to use by doing:

pd.read_csv('movies.dat', sep = '::')

This should work.

$\endgroup$
1
  • $\begingroup$ this worked movies = pd.read_csv('movies.dat', sep='::', engine='python'). $\endgroup$ – Van Peer May 6 '18 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.