我在名为 results 的变量中得到以下结果:
data = """
0 a b this is my first file
1 c d this is my second file
2 e f this is my third file
3 g h this is my fourth file
4 i j this is my fifth file
"""
我想将结果解析为 pandas DataFrame。 我想要的结果是
调用read_csv
相反,当我打电话时:
read_csv(StringIO(results), delim_whitespace=True),我得到:
有没有办法在使用 delim_whitespace 时指定最大分隔符数量?
最佳答案
# Data
results = """a b this is my first file
c d this is my second file
e f this is my third file
g h this is my fourth file
i j this is my fifth file"""
lines = results.split("\n")
words = [line.split(maxsplit=2) for line in lines]
df = pd.DataFrame(words)
关于python - 使用 delim_whitespace、read_csv 指定最大分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69619031/