我是python的新手,正在尝试以所需的布局合并multipe文件。 我的问题之一涉及重复csv文件的每一行(不包括标头)n次。例如
初始文件:
header1 header2 header3
abc 123 a1
def 456 b1
ghi 789 c1
修改后,文件应该看起来像(例如3次重复):
header1 header2 header3
abc 123 a1
abc 123 a1
abc 123 a1
def 456 b1
def 456 b1
def 456 b1
ghi 789 c1
ghi 789 c1
ghi 789 c1
使用csv或pandas在python中执行此操作的最佳方法是什么。 道歉,如果这个问题太琐碎,但是我对使用python进行此类文件操作不熟悉,并且在论坛上没有发现任何类似的问题。
谢谢
答案 0 :(得分:1)
3次,您可以这样做:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>638</width>
<height>433</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="styleSheet">
<string notr="true">background:#2A2E30;</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="BClose">
<property name="geometry">
<rect>
<x>600</x>
<y>0</y>
<width>41</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>11</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">QPushButton#BClose{
background:none;
border:0px;
color:white;
}
</string>
</property>
<property name="text">
<string>X</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>30</x>
<y>30</y>
<width>81</width>
<height>81</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background:none;</string>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap>Src_School_Admin/Img/Logo.png</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>120</x>
<y>40</y>
<width>121</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>11</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">color:#4253F4;
background:none;</string>
</property>
<property name="text">
<string>School Admin</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>120</x>
<y>60</y>
<width>201</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>10</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">color:#424D99;
background:none;</string>
</property>
<property name="text">
<string>School Administration System</string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>5</x>
<y>410</y>
<width>201</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>8</pointsize>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">color:grey;
background:none;</string>
</property>
<property name="text">
<string>2019 v 0.1 Beta - Rev.1.0</string>
</property>
</widget>
<widget class="QPushButton" name="BIniciar">
<property name="geometry">
<rect>
<x>250</x>
<y>270</y>
<width>141</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>9</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">QPushButton#BIniciar{
color:white;
background:qlineargradient(spread:pad, x1:0.475435, y1:0, x2:0.495, y2:1, stop:0 rgba(66, 83, 224, 255), stop:1 rgba(66, 77, 153, 255));
border:0px;
}
QPushButton#BIniciar:hover{
background:qlineargradient(spread:pad, x1:0.509, y1:1, x2:0.506299, y2:0, stop:0 rgba(66, 83, 224, 255), stop:1 rgba(66, 77, 153, 255));
}</string>
</property>
<property name="text">
<string>Instalar</string>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x>250</x>
<y>190</y>
<width>141</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>11</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">color:lightgrey;</string>
</property>
<property name="text">
<string>Bienvenido</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QProgressBar" name="progressBar">
<property name="geometry">
<rect>
<x>20</x>
<y>280</y>
<width>601</width>
<height>23</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">QProgressBar#progressBar::horizontal{
border:0px;
background:grey;
}
QProgressBar#progressBar::chunk{
border:0px;
background-color:qlineargradient(spread:pad, x1:1, y1:0.54, x2:0, y2:0.5, stop:0 rgba(45, 116, 185, 193), stop:1 rgba(26, 44, 53, 255));
}</string>
</property>
<property name="value">
<number>0</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property>
<property name="invertedAppearance">
<bool>false</bool>
</property>
</widget>
<widget class="QLabel" name="LEstado">
<property name="geometry">
<rect>
<x>240</x>
<y>320</y>
<width>171</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>9</pointsize>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">color:#4253F4;
background:none;</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
您可以轻松编写一个循环执行此操作n次。
答案 1 :(得分:0)
使用csv
模块,您可以执行以下操作:
import csv
with open('out.txt', 'w') as fout, open('in.txt', 'r') as fin:
reader = csv.reader(fin)
writer = csv.writer(fout)
writer.writerow(next(reader))
for l in reader:
for i in range(3):
writer.writerow(l)