You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从DataFrame将大TXT转CSV?列合并异常求助

Fix: All Columns Merged into One When Converting TXT to CSV with Pandas

Hey there, let's get to the bottom of why your data is squishing into a single column instead of the 18650 columns you expect!

The Root Cause

Looking at your actual output, you can see values are separated by \t (tab characters), but in your code you told pandas to use ; as the delimiter. Since pandas couldn't find any semicolons in your txt file, it treated every entire row as a single column.

The Fix

Update your read_csv call to use the correct delimiter—tab characters (\t). You can use either sep='\t' or delimiter='\t' (they’re interchangeable in pandas). Also, your file path has a small redundancy: when using os.path.join, if the second argument is an absolute path, it overrides the first one, so we can clean that up too.

Here's the corrected code:

import os
import pandas as pd

save_path = "/Users/Desktop/Python-testing"
# Use the direct absolute path for your input file (or fix the join if needed)
in_filename = "/Users/Desktop/Python-testing/data_gene_sample_report.txt"
out_filename = os.path.join(save_path, 'Output.csv')

# Use tab as the separator to split columns correctly
df = pd.read_csv(in_filename, sep='\t')
df.to_csv(out_filename, index=False)

Bonus Tips

  • If you’re ever unsure about the delimiter in your file, try sep='\s+' to match any whitespace (tabs or spaces), or use pd.read_table() which defaults to tab-separated data.
  • Double-check your input file’s structure by opening it in a text editor to confirm the separator type—this will save you time debugging!

内容的提问来源于stack exchange,提问作者user10108293

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:38:40