-1

alright, I apologize for the dumb question but I would really appreciate some help on this one. I am trying to analyze a cash game I played in, but seem to be having some trouble. I can get the file to open and read but when I reference it to search in it I come up with a lot of issues. The title is the current issue I am having which was implemented because it seemed that it might serve as a solution to another problem I was having. Thanks for any help in advance!

(the natural formatting of each line of code on here didn't translate well so the site is making me link to the paste of the picture instead. The lines of code are still below however.)

enter image description here

#find the nunmeber of hands played in this cash game
import pandas as pd
import os
import pprint

dataDirectory = r'C:\ACR\ACR CASHGAME 10 25.txt'
handHistory = pd.read_csv(dataDirectory, sep='\n')

handHistory = os.path.join(r'C:\ACR\ACR CASHGAME 10 25.txt', handHistory)
#when I remove the line above from the code, it says dataDirectory file does not exist even though it reads it in previous lines fine.


numberofHands = pd.read_csv('dataDirectory',dtype=object)
for key in handhistory:
    if handHistory[key] == 'Hand':
        numberofHands.count(key)
karthikr
  • 97,368
  • 26
  • 197
  • 188
  • Your question is somewhat confusing. You're using `read_csv()` twice and assigning it to two different variables with the same dataDirectory string, one of which you then reassign to an os path. Can you a) include the full error traceback and b)have a look at [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and provide sample data and expected output so that we can better understand what you're trying to do? – G. Anderson Mar 17 '21 at 19:22

1 Answers1

0

os.path.join Error

You are seeing this error because in this line

handHistory = pd.read_csv(dataDirectory, sep='\n')

You define handHistory to be a pandas.DataFrame (pandas.read_csv() returns pandas.DataFrame), whereas in this line:

handHistory = os.path.join(r'C:\ACR\ACR CASHGAME 10 25.txt', handHistory)

you are calling os.path.join() which, as per documentation:

Join[s] one or more path components intelligently.

It expects the path components to be path-like objects where:

A path-like object is either a str or bytes object representing a path, or an object implementing the os.PathLike protocol

while you are passing in a DataFrame.

This cannot work because os.path.join() doesn't know what to do with a DataFrame.

It is also not clear what this line is meant to achieve and I would suggest removing or rewriting it.

dataDirectory Error

The error that you are getting when removing the line

handHistory = pd.read_csv(dataDirectory, sep='\n')

happens because in the next line:

numberofHands = pd.read_csv('dataDirectory',dtype=object)

you are passing in 'dataDirectory' as a string (in single quotes ') into pandas.read_csv() which expects a path or file object.

Try removing the single quotes, i.e.:

numberofHands = pd.read_csv(dataDirectory, dtype=object)
Paul P
  • 3,346
  • 2
  • 12
  • 26