[英]Space delimiter in CSV import to Python
我知道有很多关于 CSV 文件中的空格分隔符的问题。
我有一个似乎由空格分隔的 CSV 文件。 导入到 Python 时,我已经尝试了所有代码以将空格标识为分隔符。 但是,我不断收到错误消息。 例如:
test_filepath = 'test_data.csv'
with codecs.open(test_filepath, "r", "Shift-JIS", "ignore") as file: # import UTF8 based csv file
test_df = pd.read_table( file, delim_whitespace=True )
这会产生以下错误:
EmptyDataError: No columns to parse from file
当我尝试这个时:
test_filepath = 'test_data.csv'
with codecs.open(test_filepath, "r", "Shift-JIS", "ignore") as file: # import UTF8 based csv file
test_df = pd.read_table( file, delimiter=" " )
它给出了同样的错误。
当我尝试这个时:
test_filepath = 'test_data.csv'
with codecs.open(test_filepath, "r", "Shift-JIS", "ignore") as file: # import UTF8 based csv file
test_df = pd.read_table( file, sep = "/s+" )
我犯了同样的错误。
当我尝试这个时:
test_filepath = 'test_data.csv'
with codecs.open(test_filepath, "r", "Shift-JIS", "ignore") as file: # import UTF8 based csv file
test_df = pd.read_table( file, delimiter='\t')
我犯了同样的错误。
如果我这样做,我不会收到错误的唯一方法是:
test_filepath = 'test_data.csv'
with codecs.open(test_filepath, "r", "Shift-JIS", "ignore") as file: # import UTF8 based csv file
test_df = pd.read_table( file, delimiter=',')
但结果看起来完全不对,test_df.info() 显示只创建了一列(应该有 100 列)。
我认为熊猫可能会成功,其中之一应该有效。
import pandas as pd
df = pd.read_csv('file.csv', delim_whitespace=True)
df = pd.read_csv('file.csv', delimiter=' ')
我知道有很多关于 CSV 文件中的空格分隔符的问题。
我有一个似乎用空格分隔的 CSV 文件。 导入 Python 时,我尝试了所有代码以将空格标识为分隔符。 但是,我不断收到错误消息。 例如:
test_filepath = 'test_data.csv'
with codecs.open(test_filepath, "r", "Shift-JIS", "ignore") as file: # import UTF8 based csv file
test_df = pd.read_table( file, delim_whitespace=True )
这会产生以下错误:
EmptyDataError: No columns to parse from file
当我尝试这个时:
test_filepath = 'test_data.csv'
with codecs.open(test_filepath, "r", "Shift-JIS", "ignore") as file: # import UTF8 based csv file
test_df = pd.read_table( file, delimiter=" " )
它给出了同样的错误。
当我尝试这个时:
test_filepath = 'test_data.csv'
with codecs.open(test_filepath, "r", "Shift-JIS", "ignore") as file: # import UTF8 based csv file
test_df = pd.read_table( file, sep = "/s+" )
我犯了同样的错误。
当我尝试这个时:
test_filepath = 'test_data.csv'
with codecs.open(test_filepath, "r", "Shift-JIS", "ignore") as file: # import UTF8 based csv file
test_df = pd.read_table( file, delimiter='\t')
我犯了同样的错误。
如果我这样做,我不会出错的唯一方法是:
test_filepath = 'test_data.csv'
with codecs.open(test_filepath, "r", "Shift-JIS", "ignore") as file: # import UTF8 based csv file
test_df = pd.read_table( file, delimiter=',')
但结果看起来完全不正确,并且 test_df.info() 显示只创建了一列(应该有 100 列)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.