0

我已经阅读了一个文本文件并将每一行转换为一个列表。

使用这个脚本:

l = [s.strip().split() for s in open("cluster2.wcnf").readlines()]

我会怎么做:

  1. 它打开的文件是动态的而不是静态的?即用户选择要打开的文件。
  2. 在转换为列表后选择要阅读的特定行。
  3. 将对象分配给列表中的值
  4. 在每行中选择第一个、最后一个或多个值?

提前致谢

4

5 回答 5

1

首先,您必须.readlines()从列表理解中删除。
其次,l是一个列表列表,访问第一行只需:l[0]。第一行的第一个元素将是l[0][0].

不过,我认为这种方法无法解决文件不断增长的问题。如果动态是指文件名,而不是文件行为,那么您可以将硬编码的文件名替换为将从用户输入中定义的变量。

于 2009-12-15T17:15:33.120 回答
0

This is a more optimised way of reading in a whole file where each line is also split into a list

import csv, sys

# 1 - User specifies file
file = input('File: ')

f = open(file, 'r')
reader = csv.reader(f, delimiter=' ')
lines = list(reader)
f.close()

# 2 - Select specific lines
print lines[4]

# 3 - Assign to list
lines[4] = obj # makes the whole line into that obj
lines[4][0] = obj # replaces the first item on the line with that obj

# 4 - Select first, last or a number of values
print lines[4][0] # first item in line 4
print lines[4][-1] # last item in line 4
print lines[4][1:4] # 2nd, 3rd and 4th items in line 4
于 2009-12-15T17:52:29.080 回答
0

The way you ask the questions you ask indicates to me that you aren't exactly clear on what you want your program to do eventually.

First, that line doesn't just convert the file to a list. It converts it to a list of tuples. Each tuple is a set of space separated values for one line in the file.

I will answer your questions as given, but I expect some of the answers may confuse you even more.

1 the file it opens is dynamic rather than static? i.e. the user chooses the file to open.

"cluster2.wcf" doesn't have to be a string. It can be a variable. If one is to read your question very literally, here is how you would modify the code to do it:

filename = raw_input("Which file would you like to read: ")
l = [s.strip().split() for s in open(filename).readlines()]

A point to note here. After you read the file into the list, the list is going to look like this:

[('This', 'is', 'the', 'first', 'line.'),
 ('This', 'is', 'the', 'second', 'line.'),
 ('And', 'finally', 'the', 'last', 'line.')]

Each element of your list is a further list of all the 'words' on that line. Words being defined as space separated groups of characters.


2 Select specific lines to read after it has been converted to a list.

This question is a confusing question. You have already read the file. Do you want to read it again? What I am guessing you actually mean is "How do I work with a specific line of the file after I've read it into a list?". I will answer this question.

Each line of the file will correspond to a particular element of the list. List elements are accessed by numbers starting with 0. The first line of your file will be in l[0]. The second in l[1] and so on up to (and this is a bit of Python magic here) l[-1]. Negative indexes in Python count backwards from the end of the list.


3 assign objects to values in the list

This one is confusing because you don't specify what kind of objects you want to assign or what you want to do with the list after you assign objects to values in it. Here is an answer that takes your question very literally...

l[0] = object()

That assigns an object to the list element that used to contain a tuple representing the first line of your file. Combining this with the previous answer I am betting you can figure out how to assign an object to any element of your list.


4 select the first, last or a number of values in each line?

This depends on exactly how you want to select them. If you want to do it individually you do it this way:

first_word_of_first_line = l[0][0]
last_word_of_first_line = l[0][-1]
first_word_of_last_line = l[-1][0]
last_word_of_last_line = l[-1][-1]
first_five_words of_second_line = l[1][0:5] # The : is for specifying a range of values
last_five_words_of_first_line = l[0][-5:] # The : with no end means 'until the end'

Unfortunately, the way to get the first 4 words of the first 2 lines is not this:

first_four_words_of_first_two_lines = l[0:2][0:4] # DOESN'T WORK!!!

The reason for that is that the result of fetching a range of values from a list is a list, and so the [0:4] applies to that list instead. What you really mean to do is to apply the [0:4] to apply to each element of the resulting list to create a new list. Here is how you would do that:

first_four_words_of_first_two_lines = [x[0:4] for x in l[0:2]]

That gives you a new list that looks like this (given the example for question 1):

[('This', 'is', 'the', 'first'),
 ('This', 'is', 'the', 'second')]

I hope that is helpful. As I said, I think your questions indicate some confusion about things. But I don't know what they indicate confusion about, so I just tried to answer them as best I could.

于 2009-12-15T17:54:16.160 回答
0

诸如使用从用户那里获得的名称(即使用字符串)打开文件以及在列表中选择/分配项目之类的事情确实是非常基本的 Python 概念。我认为你能做的最好的事情就是阅读一本关于 Python 的好书。我会推荐Dive Into Python

于 2009-12-15T17:26:13.113 回答
0

数字 1:当您说您希望它打开的文件是动态的时,您的意思是您希望用户输入要打开的文件名吗?如果是这样,您可以简单地将文件名存储到变量中。例如:

name = "myfile.txt"

并用名称替换您的“cluster2.wcnf”。

4号:

您可以使用 SilentGhost 提到的索引来索引每行中的第一项(即l[0][0]。要查看最后一项,您可以使用负索引;l[0][-1]将为您提供第一个列表中的最后一项。如果您想要一系列项目您可以使用类似的东西l[0][2:6],它将为您提供索引 2、3、4 和 5 的项目。即范围 [2,6) 中的项目。

于 2009-12-15T17:28:16.993 回答