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

CSV导入到Python中的空格分隔符,python,pandas,csv,delimiter,separator,Python,Pandas,Csv,Delimiter,Separator,我知道关于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_

我知道关于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=" " )
    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=' ')

我想熊猫可能会玩这个把戏,其中一个应该有用

import pandas as pd

df = pd.read_csv('file.csv', delim_whitespace=True)  
df = pd.read_csv('file.csv', delimiter=' ')

您可能只需使用csv模块即可完成所需的操作:

import csv
import pandas as pd

with open("test_data.csv", "r") as file:
    data = csv.reader(file, delimiter=" ")

    # Perform what you need to do on data
    for row in data:
        print(row)

    # Can then load into a df if needed
    df = pd.DataFrame.from_records(data)
    print(df)

您可能只需使用csv模块即可完成所需的操作:

import csv
import pandas as pd

with open("test_data.csv", "r") as file:
    data = csv.reader(file, delimiter=" ")

    # Perform what you need to do on data
    for row in data:
        print(row)

    # Can then load into a df if needed
    df = pd.DataFrame.from_records(data)
    print(df)

它有标题吗?您如何知道您的数据是以空格分隔的?您是否已打开数据并查看?您的回溯似乎认为您没有任何数据。请在excel中打开它并将其设为空格分隔符,而不是疯狂地猜测您有什么类型的空白,为什么不实际读取其中一行(只是
row=file.readline()
),然后
print repr(row)
,然后实际查看您有什么,你需要更仔细地阅读这些文件
/s+/
没有任何意义,或者说,它意味着要用斜杠分隔列,然后用一个或多个字母
s
s分隔列,这不是很有用。它有标题吗?您如何知道数据是以空格分隔的?您是否已打开数据并查看?您的回溯似乎认为您没有任何数据。请在excel中打开它并将其设为空格分隔符,而不是疯狂地猜测您有什么类型的空白,为什么不实际读取其中一行(只是
row=file.readline()
),然后
print repr(row)
,然后实际查看您有什么,你需要更仔细地阅读这些文件
/s+
没有任何意义,或者说,它意味着要用斜杠分隔列,然后用一个或多个字母
s
s分隔列,这不是很有用。