CSV 导入到 Python 中的空格分隔符

pyn*_*bee 1 python csv delimiter separator pandas

我知道有很多关于 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 )
Run Code Online (Sandbox Code Playgroud)

这会产生以下错误:

EmptyDataError: No columns to parse from file
Run Code Online (Sandbox Code Playgroud)

当我尝试这个时:

    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=" " )
Run Code Online (Sandbox Code Playgroud)

它给出了同样的错误。

当我尝试这个时:

    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+" )
Run Code Online (Sandbox Code Playgroud)

我犯了同样的错误。

当我尝试这个时:

        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')
Run Code Online (Sandbox Code Playgroud)

我犯了同样的错误。

如果我这样做,我不会出错的唯一方法是:

        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=',')
Run Code Online (Sandbox Code Playgroud)

但结果看起来完全不正确,并且 test_df.info() 显示只创建了一列(应该有 100 列)。

kso*_*all 5

我认为熊猫可能会成功,其中之一应该可以。

import pandas as pd

df = pd.read_csv('file.csv', delim_whitespace=True)  
df = pd.read_csv('file.csv', delimiter=' ')
Run Code Online (Sandbox Code Playgroud)