我有一个csv文件,需要分割成两个csv文件(file1.csv和file2.csv)。应根据列“Name”进行拆分。70%的行需要写入file1.csv,其余30%写入file2.csv。例如,有10行名为“AAA”。所以10行中的70%意味着前7行“AAA”需要写入file1.csv,接下来的3行写入file2.csv。像这样,“Name”列下的所有名称都需要这样做。 如果得到一个十进制数,例如0.7 x 9行=6.3。然后前6行(四舍五入)到file1.csv,其余3行到file2.csv 如何使用Python代码实现这一点?谢谢你https://fil.email/FPYB1RWd
很简单:制作一个包含记录列表的字典,输入给定列的内容(这里我使用的是第0列),然后循环这些记录 根据OP指定的简单规则,列出并输出包含在两个输出文件之一的记录列表中的每个记录。在
布尔值是整数的子类,其值为1(当
n<l
)或0时,可用于索引writer
s的列表。下面是使用IPython会话(稍微 编辑以减少空白)和一些人工数据
^{pr2}$如您所见,第一个文件将获得原始记录的70%,而 第二个得到剩下的30%。在
“” 用你的原始文件名代替你的“文件名” 使用此函数,该函数将拆分csv文件并保存。 您可以更改拆分百分比以获得不同的文件大小 “”
读取整个csv文件并将内容存储在一个列表中。然后在临时列表中存储类似的csv数据。存储后,从列表中提取70%的数据并将其写入文件,然后将剩余的数据写入另一个文件。在
文件1.csv
文件2.csv
相关问题 更多 >
编程相关推荐