python读取txt文档、用户输入用户名、如有则输入密码,PYTHON:如何使用PYTHON中的原始输入将.txt文件作为用户的输入,然后按行读取该文件?...

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()

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值