我正在使用 google colab,我的驱动器中有这样的文件:
M0000.csv
M0001.csv
M0002.csv
.
.
.
M0099.csv
我需要循环输入 100 个文件,每 10 个文件我就必须做一些事情。我需要将所有文本保存在 1 个列表数组中的 10 个文件中,如下所示:
all_text[0] = list of text in file from 1 to 10
.
.
all_text[9] = list of text in file from 91 to 100
这是我在所有文件中循环的代码(不循环每个 10 个文件——我不知道如何):
dir = 'drive/My Drive/Tri/'
pd.options.display.max_colwidth = 5000
#Loop for all file
for file in sorted(glob.glob(dir + "*.csv")):
print(f"File: {file}")
# Check the number of columns in the file
df = pd.read_fwf(file, header=None, on_bad_lines='skip', delimiter="\n")
# Loop inside each file
for i in range(len(df)): # Loop over the rows ('i')
#code to do
print("All Text:", all_text)
跟踪您已处理的文件数量。如果这个数字能被十整除,就做你额外的事情。
filenumber = 0
for file in sorted(glob.glob(dir + "*.csv")):
filenumber += 1
if filenumber % 10 == 0:
# do your extra thing