PYTHON:如何使用raw_input从用户获取.txt文件并逐行读取?

3 投票
2 回答
24019 浏览
提问于 2025-04-16 23:50

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

一旦获取了输入,我想访问用户提供的这个 txt 文件,并把里面的内容写入另一个文件(每一行写在下一行)。

我遇到的问题是,怎么把上面提到的两个步骤连接起来,或者说怎么把用户输入和读取文件的过程联系起来。我需要帮助……以下是我的代码(不过有错误 :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()

我遇到的错误是:

无法将 'str' 和 'list' 对象连接在一起

2 个回答

0

下面是修正后的代码

import sys,os,csv

x = raw_input("Enter name of file to be written row wise:")
input_filename = x + ".txt"
input_file = open(input_filename)
outfile = open("myfile.csv","wb")
out = csv.writer(outfile)
for row in input_file:
    row = row.strip().split(';')
    if row:
        for subrow in row:
            subrow = subrow.strip().split()
            if subrow:
                out.writerow(subrow)
input_file.close()
outfile.close()
3
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() 的话,会把 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 只有空格的情况下会返回一个空列表,因为 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

.

最后,我想告诉你,使用正则表达式的代码会更高效:

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

撰写回答