绘制一个 CSV 文件,其中分隔符是 ';'(分号 + 空格) [英] Plot a CSV file where the delimiter is '; ' (semicolon + space)

查看:69
本文介绍了绘制一个 CSV 文件,其中分隔符是 ';'(分号 + 空格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用 import matplotlib.pyplot as plt 在Python中绘制事物(CSV文件).

I'm learning how to plot things (CSV files) in Python, using import matplotlib.pyplot as plt.

Column1;Column2;Column3;
1;4;6;
2;2;6;
3;3;8;
4;1;1;
5;4;2;

我可以用 plt.plotfile('test0.csv', (0, 1), delimiter=';') 绘制上图,得到下图.

I can plot the one above with plt.plotfile('test0.csv', (0, 1), delimiter=';'), getting the figure below.

如果我将分隔符从 ';'(分号)更改为 ','(逗号),我也可以绘制该数据.

I can also plot that data if I change the separator from ';' (semicolon) to ',' (comma).

Column1,Column2,Column3,
1,4,6,
2,2,6,
3,3,8,
4,1,1,
5,4,2,

使用 plt.plotfile('test0.csv', (0, 1), delimiter=',').

但是没有设法在分隔符为'的地方绘制数据;(分号+空格),如下所示.我还可以用 matplotlib.pyplot 拍这个还是该做点别的了?

But didn't managed to plot data where the separator is '; ' (semicolon + space), as show below. Can I still shoot this with matplotlib.pyplot or it's time to something else?

Column1; Column2; Column3; 
1; 4; 6; 
2; 2; 6; 
3; 3; 8; 
4; 1; 1; 
5; 4; 2; 

推荐答案

所以 matplotlib 抛出的错误是

So the error matplotlib is throwing you is

 TypeError: "delimiter" must be a 1-character string

这使得您似乎不太可能使用';'.尝试 delimiter =';'时,我也抛出了错误,尽管您可能希望检查一下它是否可重现.

Which makes it seem very unlikely you can use '; '. I also had errors thrown when I tried delimiter=';' although you may wish to check that that is reproducible.

但是,如果可用,pandas可以使用 pd.read_csv 很好地处理此问题.

However pandas handles this just fine with pd.read_csv if you have it available.

import pandas as pd
alpha = pd.read_csv(filepath,delimiter=';')
alpha.Column1
0    1
1    2
2    3
3    4
4    5
Name: Column1, dtype: int64

这篇关于绘制一个 CSV 文件,其中分隔符是 ';'(分号 + 空格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆