for row in data:
row = row.strip().split(';')
执行split(';')的结果是,根据“;”拆分的行(应该写入:line)总是给出一个非空列表,即使它是一个空行,甚至在使用strip():''.split(';')剥离后也会给出['']。所以你的下列条件if row:是无用的。
这意味着您的代码相当于:for row in data:
row = row.strip().split(';')
for subrow in row:
subrow = subrow.split()
if subrow:
out.writerow(subrow)
然后是:for row in data:
for subrow in row.strip().split(';'):
subrow = subrow.split()
if subrow:
out.writerow(subrow)
是的。
此外,使用列表中的subrowrow.strip().split(';')上的split()可以消除subrow中每个单词前后的所有空白。所以row.strip().split(';')中的第一个strip()也没用。
您的代码相当于:for row in data:
for subrow in row.split(';'):
subrow = subrow.split()
if subrow:
out.writerow(subrow)
现在,subrow.split()可以在subrow仅为空时生成一个void列表,因为没有参数的split()有其特殊的算法。所以指令if subrow是有用的。
是的。
实际上,您的代码所做的是,在阅读了这样一个文件的内容之后:Blackcurrant, Redcurrant ; Orange ; Blueberry
Pear;Chestnut; Lemon Lime, Grapefruit
Apple;Apricot ; Pineapple, Fig; Mulberry, Hedge Apple
要录制另一个类似的文件:Blackcurrant
Redcurrant
Orange
Blueberry
Pear
Chestnut
Lemon Lime
Grapefruit
Apple
Apricot
Pineapple
Fig
Mulberry
Hedge
Apple
我更喜欢下面的代码:filename = raw_input("Enter name of file to be written row wise:") + '.txt'
filepath = 'I:\\' + filename
with open(filepath) as handler,open("myfile.csv","wb") as outfile:
out = csv.writer(outfile)
for row in handler:
gen = ( subrow.split() for subrow in row.split(';') )
out.writerow([x for x in gen if x])
del out
是的。
这段代码将一直运行,即使对于内容无法由内存保存的非常大的文件,因为文件的行是一行接一行读取的。
如果文件不是那么大,可以像您那样继续,使用readlines():with open(filepath) as handler:
data = handler.readlines()
with open("myfile.csv","wb") as outfile:
out = csv.writer(outfile)
for row in data:
gen = ( subrow.split() for subrow in row.split(';') )
out.writerow([x for x in gen if x])
del out
但是没有特别的兴趣来继续,所以您也可以做for row in handler。
是的。
个人而言,我认为最好使用writerows():filename = raw_input("Enter name of file to be written row wise:") + '.txt'
filepath = 'I:\\' + filename
with open(filepath) as handler,open("myfile.csv","wb") as outfile:
out = csv.writer(outfile)
gen = ( x for row in handler for x in (subrow.split() for subrow in row.split(';')) )
out.writerows([x for x in gen if])
del out
是的。
最后,我要告诉您,使用regex的代码效率要高得多:import csv, re
regx = re.compile('[ ;\r\n]+')
filename = raw_input("Enter name of file to be written row wise:") + '.txt'
filepath = 'I:\\' + filename
with open(filepath) as handler,open("myfile.txt","w") as outfile:
outfile.write('\n'.join(x for x in regx.split(handler.read()) if x))
编辑1handler = open(filepath)
outfile = open("myfile.txt","wb")
out = csv.writer(outfile)
for row in handler:
gen = ( subrow.split() for subrow in row.split(';') )
out.writerow([x for x in gen if x])
del out
outfile.close()
handler.close()
或者import csv, re
regx = re.compile('[ ;\r\n]+')
filename = raw_input("Enter name of file to be written row wise:") + '.txt'
filepath = 'I:\\' + filename
handler = open(filepath)
outfile = open("myfile.txt","w")
outfile.write('\n'.join(x for x in regx.split(handler.read()) if x))
outfile.close()
handler.close()