--I want to load csv.
--Character code varies depending on the file
--There is ␣
in the file name
Story of time
import pathlib
import subprocess as sp
path_file = pathlib.Path('hoge hoge.csv')
encoding = sp.getoutput('nkf -g ' + str(path_file))
df = pd.read_csv(path_file, encoding=encoding)
Anyway, there is an error in the subprocess
import pathlib
import subprocess as sp
path_file = pathlib.Path('hoge\ hoge.csv')
encoding = sp.getoutput('nkf -g ' + str(path_file))
df = pd.read_csv(path_file, encoding=encoding)
(The space between hoge
and hoge
is \ ␣
)
Anyway, there is an error in pd.read_csv
Angry! !! !! !! !!
Solved by hard-coding anger (although not so hard)
import pathlib
import subprocess as sp
path_file = pathlib.Path('hoge hoge.csv')
encoding = sp.getoutput('nkf -g ' + str(path_file).replace(' ', '\ ')))
df = pd.read_csv(path_file, encoding=encoding)
I just replaced ␣
with \ ␣
.
can not forgive…!
Why is there a ␣
in the folder name that the system automatically puts in?
I ... I want to erase ␣, which is confused in all file names, before I am born. With this hand, ␣, which is confused with all the file names of all universes, past and future.
Postscript (2017/06/01 17:50)
With reference to @ shiracamus's comment
encoding = sp.check_output(['nkf', '-g', str(path_file)])
If so, it worked.
I can't tell the difference between get output
and check_output
by reading the documentation ...
I don't have enough study.
Recommended Posts