Can';t在python中打开CSV文件
我运行了以下脚本()并在末尾添加了以下行以下载文件:Can';t在python中打开CSV文件,python,pandas,csv,Python,Pandas,Csv,我运行了以下脚本()并在末尾添加了以下行以下载文件: output_folder = '/Users/me/Documents/data/forex/' target_folder = os.path.join(output_folder, symbol, year) os.makedirs(target_folder, exist_ok=True) with open(os.path.join(target_folder, str(i) + '.csv'), '
output_folder = '/Users/me/Documents/data/forex/'
target_folder = os.path.join(output_folder, symbol, year)
os.makedirs(target_folder, exist_ok=True)
with open(os.path.join(target_folder, str(i) + '.csv'), 'wb') as outfile:
outfile.write(data)
然后,我尝试使用pandas打开文件,如下所示:
x = pd.read_csv('/Users/me/Documents/data/forex/EURUSD/2015/29.csv')
然而,我得到的是:
In [3]: x.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2415632 entries, 0 to 2415631
Data columns (total 3 columns):
D float64
Unnamed: 1 float64
Unnamed: 2 float64
dtypes: float64(3)
memory usage: 55.3 MB
In [4]: x.dropna()
Out[4]:
Empty DataFrame
Columns: [D, Unnamed: 1, Unnamed: 2]
Index: []
感谢您提供了一个非常具体且可重复的问题 我粘贴了你的代码并在windows中运行,它实际上只读取了55MB的空值 但我认为这是熊猫没有正确解析csv文件的问题,而不是它无法打开csv文件 但是,在我尝试了中列出的所有编码之后,它根本没有产生效果,因此文件可能也有问题
我最终是通过在excel中打开它并另存为不同的文件,然后pandas才能正确解析它。显然,数据中的每个字符后面都跟有空字符
\x00
。摆脱它们,事情就会发生:
outfile.write(data.replace(b'\x00',b''))
在删除空值之前,数据帧不是空的。您需要使用parse_dates='DateTime'
outfile.write(data.replace(b'\x00',b''))