繁体   English   中英

如何将文本文件(包含“,”作为分隔符)转换为熊猫数据框

[英]How do i convert a text file (containing "," as seperator) to a pandas dataframe

我正在尝试读取包含以下内容的文本文件(大约 100 万行):

第一行: “column_header”、“column_header”、“column_header”、“column_header”

从第二行开始: “价值”、“价值”、“价值”、“价值”

我尝试了以下方法:

''' try 1 '''
with open(file, 'rt') as f:
    contents = f.readlines()

for i in contents:
    print(i) # ->> seeing the text as ," value ", " value ", "
    x = [_.strip().replace('""', '').split(',') for _ in i]
    print(str(x)) # ->> getting bytez

''' try 2 '''
with open(file, 'rt') as f:
    contents = f.read()

    for i in contents:
        print(str(i)) # ->> text but cannot do anything

''' try 3 '''
frame = pd.read_csv(file, sep=',', doublequote=True, skip_blank_lines=True) # ->> utf parsing error

我发现我收到的文本文件没有编码 utc-8。 因此,以上都没有奏效。 我的解决方案:打开并另存为 .txt(utf8 编码)。 比使用以下 python 代码:

file = folder_location + 'report.txt'

''' try 3 '''
frame = pd.read_csv(file, sep=',', doublequote=True, skip_blank_lines=True)
print(frame.head())

暂无
暂无

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

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