【问题标题】:Read First column of xlsx file create Txt files than Put second column data to corresonding txt file读取 xlsx 文件的第一列创建 Txt 文件而不是将第二列数据放入相应的 txt 文件
【发布时间】:2022-01-27 17:50:10
【问题描述】:

例如,我的 xlsx 文件估计有 20000 行和 2 列;

station feedback
1   aax2a3x9564
1   a52a42x366x
1   29a9a756022
1   a6645207742
2   a7692202650
2   a64399x05a6
2   a9909a2x470
2   32654440a54
4   a9306aa7244
4   2a9a303342x
4   a66a52054xx
9   424272a64x0
9   aa4733a3a60
10  4225034a090
10  a9a29054x32

我想使用第一列(站)作为 Txt 文件名,第二列(反馈)作为数据?

我怎样才能做到这一点?

【问题讨论】:

  • 您尝试了哪些方法,遇到了哪些具体问题?
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: python xlsx txt


【解决方案1】:

您可以使用pandasopenpyxl 轻松做到这一点:

# Python env: pip install pandas openpyxl
# Anaconda env: conda install pandas openpyxl
import pandas as pd

df = pd.read_excel('data.xlsx')
for station, data in df.groupby('station'):
    with open(f'{station}.txt', 'w') as fp:
        fp.writelines('\n'.join(data['feedback'].tolist()))

1.txt的内容:

aax2a3x9564
a52a42x366x
29a9a756022
a6645207742

2.txt的内容:

a7692202650
a64399x05a6
a9909a2x470
32654440a54

【讨论】:

  • 感谢您的关注和帮助我的解决方案 df = pd.read_excel('data.xlsx') for station, data in df.groupby('alan'): with open(f'{station} .txt', 'w') as fp: for item in data['kimlik']: fp.write("%s\n" % item)
  • 很高兴读到。如果这适合您的需要,请考虑accept my answer :)
猜你喜欢
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-12
  • 2022-07-17
  • 2018-08-21
  • 2020-11-11
相关资源
最近更新 更多