寻找一个可以为atxt文件添加行号的Python程序/工具 [英] Looking for a Python Program/Tool That Will Add Line Numbers to atxt File

查看:79
本文介绍了寻找一个可以为atxt文件添加行号的Python程序/工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

见主题。它是一个简单的txt文件,每一行都是Python stmt,但我需要

最多四位数添加到每一行,数字字段之间有一个空格

和文本。也许有人已经这样做过了,或者网上有一个来源。/ b
。我还没有使用Python的文件。我的突然需求已经突然爆发了。我正在使用Win XP。

-

Wayne Watson(加利福尼亚州内华达市)


网页:< ; speckledwithStars.net>

See Subject. It''s a simple txt file, each line is a Python stmt, but I need
up to four digits added to each line with a space between the number field
and the text. Perhaps someone has already done this or there''s a source on
the web for it. I''m not yet into files with Python. A sudden need has burst
upon me. I''m using Win XP.
--
Wayne Watson (Nevada City, CA)

Web Page: <speckledwithStars.net>

推荐答案

2月14日上午8:54,W。沃森" < wolf_tra ... @ invalid.comwrote:
On Feb 14, 8:54 am, "W. Watson" <wolf_tra...@invalid.comwrote:

参见主题。它是一个简单的txt文件,每一行都是Python stmt,但我需要

最多四位数添加到每一行,数字字段之间有一个空格

和文本。也许有人已经这样做过了,或者网上有一个来源。/ b
。我还没有使用Python的文件。我的突然需求已经突然爆发了。我正在使用Win XP。

-

Wayne Watson(加利福尼亚州内华达市)


网页:< ; speckledwithStars.net>
See Subject. It''s a simple txt file, each line is a Python stmt, but I need
up to four digits added to each line with a space between the number field
and the text. Perhaps someone has already done this or there''s a source on
the web for it. I''m not yet into files with Python. A sudden need has burst
upon me. I''m using Win XP.
--
Wayne Watson (Nevada City, CA)

Web Page: <speckledwithStars.net>



通过该文件枚举将产生一个计数器(从@

零开始,只需加1)并使用字符串函数.zfill ()填写

给你。

例如。

枚举中的(line_cnt,each_line)
(input_file) :

output_file.write(print(''%s''%(line_cnt + 1))。zfill(5)+ each_line)

output_file.close()

input_file.close()

enumerate through the file which will yield you a counter (starting @
zero so just add 1) and use the string function .zfill() to pad it out
for you.
eg.

for (line_cnt, each_line) in enumerate(input_file):
output_file.write(print (''%s ''%(line_cnt+1)).zfill(5) + each_line)
output_file.close()
input_file.close()


2月14日下午6:13,Chris< cwi ... @ gmail.comwrote:
On Feb 14, 6:13 pm, Chris <cwi...@gmail.comwrote:

2月14日上午8:54,W。沃森" < wolf_tra ... @ invalid.comwrote:
On Feb 14, 8:54 am, "W. Watson" <wolf_tra...@invalid.comwrote:

参见主题。它是一个简单的txt文件,每一行都是Python stmt,但我需要

最多四位数添加到每一行,数字字段之间有一个空格

和文本。也许有人已经这样做过了,或者网上有一个来源。/ b
。我还没有使用Python的文件。我的突然需求已经突然爆发了。我正在使用Win XP。

-

Wayne Watson(加利福尼亚州内华达市)
See Subject. It''s a simple txt file, each line is a Python stmt, but I need
up to four digits added to each line with a space between the number field
and the text. Perhaps someone has already done this or there''s a source on
the web for it. I''m not yet into files with Python. A sudden need has burst
upon me. I''m using Win XP.
--
Wayne Watson (Nevada City, CA)


网页:< speckledwithStars.net>
Web Page: <speckledwithStars.net>



通过该文件枚举将产生一个计数器(从@

零开始,只需加1)并使用字符串函数.zfill ()填写

给你。

例如。

枚举中的(line_cnt,each_line)
(input_file) :

output_file.write(print(''%s''%(line_cnt + 1))。zfill(5)+ each_line)


enumerate through the file which will yield you a counter (starting @
zero so just add 1) and use the string function .zfill() to pad it out
for you.
eg.

for (line_cnt, each_line) in enumerate(input_file):
output_file.write(print (''%s ''%(line_cnt+1)).zfill(5) + each_line)



(1)那是什么打印在那里做什么?

(2)zfill(5)? OP要求最多4位数字,而不是5.

(2)作为str.zfill的替代方案,请考虑使用格式:


output_file.write(''%04d%s''%(line_cnt + 1,each_line))


什么是最多4意思?什么OP想要第10000行

看起来像什么?

(1) What''s that "print" doing in there?
(2) zfill(5)? The OP asked for "up to 4 digits", not 5.
(2) As an alternative to str.zfill, consider using formatting:

output_file.write(''%04d %s'' % (line_cnt+1, each_line))

And what does "up to 4" mean? What does the OP want the 10000th line
to look like?


2月14日下午1:29,John Machin < sjmac ... @ lexicon.netwrote:
On Feb 14, 1:29 pm, John Machin <sjmac...@lexicon.netwrote:

2月14日下午6:13,Chris< cwi ... @ gmail.comwrote:
On Feb 14, 6:13 pm, Chris <cwi...@gmail.comwrote:

2月14日上午8:54,W。沃森" < wolf_tra ... @ invalid.comwrote:
On Feb 14, 8:54 am, "W. Watson" <wolf_tra...@invalid.comwrote:


参见主题。它是一个简单的txt文件,每一行都是Python stmt,但我需要

最多四位数添加到每一行,数字字段之间有一个空格

和文本。也许有人已经这样做过了,或者网上有一个来源。/ b
。我还没有使用Python的文件。我的突然需求已经突然爆发了。我正在使用Win XP。

-

Wayne Watson(加利福尼亚州内华达市)
See Subject. It''s a simple txt file, each line is a Python stmt, but I need
up to four digits added to each line with a space between the number field
and the text. Perhaps someone has already done this or there''s a source on
the web for it. I''m not yet into files with Python. A sudden need has burst
upon me. I''m using Win XP.
--
Wayne Watson (Nevada City, CA)


网页:< speckledwithStars.net>
Web Page: <speckledwithStars.net>


枚举文件将产生一个计数器(从@

零开始,所以只需加1 )并使用字符串函数.zfill()来填充

给你。

例如。
enumerate through the file which will yield you a counter (starting @
zero so just add 1) and use the string function .zfill() to pad it out
for you.
eg.


for(line_cnt,each_line)枚举(input_file):

output_file.write(print(''%s) ''%(line_cnt + 1))。zfill(5)+ each_line)
for (line_cnt, each_line) in enumerate(input_file):
output_file.write(print (''%s ''%(line_cnt+1)).zfill(5) + each_line)



(1)那是什么" print"在那里做什么?

(2)zfill(5)? OP要求最多4位数字,而不是5.

(2)作为str.zfill的替代方案,请考虑使用格式:


output_file.write(''%04d%s''%(line_cnt + 1,each_line))


什么是最多4意思?什么OP想要第10000行

看起来像什么?


(1) What''s that "print" doing in there?
(2) zfill(5)? The OP asked for "up to 4 digits", not 5.
(2) As an alternative to str.zfill, consider using formatting:

output_file.write(''%04d %s'' % (line_cnt+1, each_line))

And what does "up to 4" mean? What does the OP want the 10000th line
to look like?



打印是一个错字

看看在zfill开火之前构建的字符串,它有

a尾随空间所以它是正确的。 ;)

print was a typo
take a look at the string that is built before the zfill fires, it has
a trailing space so it is correct. ;)


这篇关于寻找一个可以为atxt文件添加行号的Python程序/工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆