【问题标题】:how to print data in 10000 line per file in text file如何在文本文件中的每个文件中打印 10000 行数据
【发布时间】:2021-02-18 16:12:21
【问题描述】:

我必须组合某些数据,这些数据在 excel 文件中可用。但以百万计的行数组合。所以我想每个文件打印 50000 行。

我的代码是。(以文本格式制作单个文件,但希望每个文件打印 50000 行)

import pandas as pd
import itertools

df = pd.read_excel("data.xlsx", header=None, index_col=False, dtype=str)
df.columns = ['A']

comb = list(itertools.product(df['A'], repeat=4))
a = comb

for x in range(len(a)):
    print(a[x], file=open('comb.txt', 'a'))

【问题讨论】:

  • 每个文件是 10000 还是 50000?
  • 查看itertools文档中的grouper配方;它可以让你在comb 上以 50,000 个为单位进行操作;你会将每个块写入一个单独的文件。

标签: python pandas printing split


【解决方案1】:
i = 0
lines = []
for item in itertools.product(df['A'], repeat=4)):
    lines.append(''.join(item))

    if len(lines) > 50000:
        with open(f'comb.{i}.txt', 'a') as f:
            f.write('\n'.join(lines))
        lines = []
        i += 1
        

【讨论】:

  • 'TypeError: sequence item 0: expected str instance',找到元组。在我的excel文件中它有代码。像AA,AB,AC......任何解决方案
猜你喜欢
  • 2012-02-05
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
  • 1970-01-01
  • 1970-01-01
  • 2013-02-03
  • 1970-01-01
  • 2011-06-13
相关资源
最近更新 更多