简体   繁体   English

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

[英]Space delimiter in CSV import to Python

I know there are more than a few questions regarding space delimiters in CSV files.我知道有很多关于 CSV 文件中的空格分隔符的问题。

I have a CSV file that appears to be separated by a space.我有一个似乎由空格分隔的 CSV 文件。 When importing to Python, I have tried every code out there to identify space as a delimiter.导入到 Python 时,我已经尝试了所有代码以将空格标识为分隔符。 However, I keep getting error messages.但是,我不断收到错误消息。 For example:例如:

    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 )

this yields the following error:这会产生以下错误:

EmptyDataError: No columns to parse from file

when I try this:当我尝试这个时:

    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=" " )

it gives the same error.它给出了同样的错误。

when i try this:当我尝试这个时:

    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+" )

I get the same error.我犯了同样的错误。

When I try this:当我尝试这个时:

        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')

I get the same error.我犯了同样的错误。

the ONLY WAY I dont get an error is if I do this:如果我这样做,我不会收到错误的唯一方法是:

        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=',')

but the results look completely off, and test_df.info() shows that only one column is created (there should be like 100 columns).但结果看起来完全不对,test_df.info() 显示只创建了一列(应该有 100 列)。

I think pandas might do the trick, one of these should work.我认为熊猫可能会成功,其中之一应该有效。

import pandas as pd

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

I know there are more than a few questions regarding space delimiters in CSV files.我知道有很多关于 CSV 文件中的空格分隔符的问题。

I have a CSV file that appears to be separated by a space.我有一个似乎用空格分隔的 CSV 文件。 When importing to Python, I have tried every code out there to identify space as a delimiter.导入 Python 时,我尝试了所有代码以将空格标识为分隔符。 However, I keep getting error messages.但是,我不断收到错误消息。 For example:例如:

    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 )

this yields the following error:这会产生以下错误:

EmptyDataError: No columns to parse from file

when I try this:当我尝试这个时:

    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=" " )

it gives the same error.它给出了同样的错误。

when i try this:当我尝试这个时:

    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+" )

I get the same error.我犯了同样的错误。

When I try this:当我尝试这个时:

        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')

I get the same error.我犯了同样的错误。

the ONLY WAY I dont get an error is if I do this:如果我这样做,我不会出错的唯一方法是:

        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=',')

but the results look completely off, and test_df.info() shows that only one column is created (there should be like 100 columns).但结果看起来完全不正确,并且 test_df.info() 显示只创建了一列(应该有 100 列)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM