[PYTHON] file name. You can have a half-width space in pathlib, but you should escape the half-width space when you hit it in the subprocess. If you use check_output, you don't need to escape.

--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

file name. You can have a half-width space in pathlib, but you should escape the half-width space when you hit it in the subprocess. If you use check_output, you don't need to escape.
If you guys in the scope kitchen can do it with a margin ~ ♪
I tried to scrape YouTube, but I can use the API, so don't do it.
What to do if you have corrected the mistake in the IP address of the zone file but cannot connect to the DNS server
If you don't know it, it's dangerous. Carefully explain how to use the xargs command
[Python] When you want to use all variables in another file
Check if you can connect to a TCP port in Python
What to do if you can't use the trash in Lubuntu 18.04.
What to do if you should have set an ssh key but are prompted for a password
What to do if you get the error RuntimeError: Python is not installed as a framework when trying to use matplitlib and pylab in Python 3.3