簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM