[英]How to read a file with a semi colon separator in pandas
I a importing a .csv
file in python with pandas.我用熊猫在 python 中导入一个.csv
文件。
Here is the file format from the .csv
:这是.csv
的文件格式:
a1;b1;c1;d1;e1;...
a2;b2;c2;d2;e2;...
.....
here is how get it :这是如何得到它:
from pandas import *
csv_path = "C:...."
data = read_csv(csv_path)
Now when I print the file I get that :现在,当我打印文件时,我得到了:
0 a1;b1;c1;d1;e1;...
1 a2;b2;c2;d2;e2;...
And so on... So I need help to read the file and split the values in columns, with the semi color character ;
等等...所以我需要帮助来读取文件并使用半色字符将值拆分为列;
. .
read_csv
takes a sep
param, in your case just pass sep=';'
read_csv
需要一个sep
参数,在你的情况下只需传递sep=';'
like so:像这样:
data = read_csv(csv_path, sep=';')
The reason it failed in your case is that the default value is ','
so it scrunched up all the columns as a single column entry.在您的情况下它失败的原因是默认值是','
因此它将所有列作为单个列条目进行整理。
In response to Morris' question above: "Is there a way to programatically tell if a CSV is separated by , or ; ?"针对上述 Morris 的问题:“有没有办法以编程方式判断 CSV 是否由 , 或 ; 分隔?”
This will tell you:这会告诉你:
import pandas as pd
df_comma = pd.read_csv(your_csv_file_path, nrows=1,sep=",")
df_semi = pd.read_csv(your_csv_file_path, nrows=1, sep=";")
if df_comma.shape[1]>df_semi.shape[1]:
print("comma delimited")
else:
print("semicolon delimited")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.