gpt4 book ai didi

PYTHON:如何在 Python 中使用 raw_input 将 .txt 文件作为用户的输入,然后逐行读取该文件?

转载 作者:行者123 更新时间:2023-11-28 16:51:17 25 4
gpt4 key购买 nike

我想通过 raw_input 从用户那里获取一个 .txt 文件作为输入。

输入完成后,我希望访问用户给出的 txt 文件名,并将其中的内容写入另一个文件(每个文件都在下一行)。

我遇到了如何连接上面的 a 和 b 或如何将原始输入与文件读取链接起来的问题。我需要帮助...代码如下(但它是错误的 :S)。

import sys,os,csv

x = raw_input("Enter name of file to be written row wise:")
ui = "x" + txt
for ui in x:
data = open("ui").readlines()
outfile = open("myfile.csv","w")
out = csv.writer(outfile)
for row in data:
row = row.strip().split(';')
if row:
for subrow in row:
subrow = subrow.strip().split()
if subrow:
out.writerow(subrow)
outfile.close()

我得到错误:

cannot concatenate 'str' and 'list' objects

最佳答案

for row in data: 
row = row.strip().split(';')

您执行 split(';') 的结果是一行(您应该写:行)根据 ';' 拆分总是给出一个非空列表,即使它是一个空行,甚至在被 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)

.

此外,您在列表 row.strip().split(';')subrow 上使用 split() strong> 消除了 subrow 中每个单词前后的所有空格。所以 row.strip().split(';') 中的第一个 strip() 也是无用的。

你的代码等同于:

for row in data:
for subrow in row.split(';'):
subrow = subrow.split()
if subrow:
out.writerow(subrow)

现在,subrow.split() 可以在子行只有空白时生成一个空列表,因为没有参数的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

.

Personnaly,我认为使用 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

.

我通过通知您使用正则表达式的代码会更有效率来结束这个回答:

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

编辑1

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

关于PYTHON:如何在 Python 中使用 raw_input 将 .txt 文件作为用户的输入,然后逐行读取该文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7102030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com