简体   繁体   中英

Append next word into a list from a text file in python and print it columnwise

number=[]
name=[]
null=[]
fh = open("foo.txt","r")
for line in fh:
 words=line.split()
 for word in words:
    if(word=="number"):
        number.append(word+1)
        print(word)
        word=word+2
    if(word=="name"):
        name.append(word+1)
        word=word+2
    else:
        null.append(word+1)
        word=word+2
print("number " " instances " " name " " instances " " null " " instances ")
print(number, len(number), name, len(name), null, len(null) )
fh.close()

This is my minimal python code. My objective here is to print columnwise the features (eg name) and the number of instances of it. My test file (foo.txt) is of the following order

name Mathew
null has
number 4
null dogs
null and
null a 
null cat

I know that my code is not right. Especially during the append statement and the increment statement. My question is: a. What would be the correct statement? b. What would i do to get a coulmnwise output, especially if there are a very large number of words, in other words, can i wrap in a column?

Expected output

number instances      name     instances     null         instances
  4        1         Mathew        1       has, dogs,         5
                                           and, a, cat

Absolute beginner here.

a. String method 'split' - return list of values, so in 'words' variable will be list of words from current line of file. When you iterate in 'words' - you iterate on every word in current line, so you don't need it.

 for line in fh:
    words=line.split()
    if (words[0] == "number"):
        number.append(int(words[1]))
        print(words[1])
    if (words[0] == "name"):
        name.append(words[1])
    else:
        null.append(words[1])

if name can contain multiple words, you can use:

name.append(" ".join(words[1:]))

if you don't need to separate 'null' values and 'numbers' from file - you can use:

elif (words[0] == "name"):

b. If you would print columnwise output, you can use string method 'format':

print("numbers: {:>20}, instances: {:>20}".format(str(number), len(number)))
print("name:    {:>20}, instances: {:>20}".format(str(name), len(name)))
print("null:    {:>20}, instances: {:>20}".format(str(null), len(null)))

This works and I have used mostly your code so that it is easy for you to understand how to improve upon:

name=[]
null=[]
number=[]
fh = open("foo.txt","r")
for line in fh:
    word= line.split()[0]
    if word == "name":
        name.append(line.rstrip('\n').split()[1])
    elif word =="number":
        number.append(line.rstrip('\n').split("number")[1])
    else:
        null.append(line.rstrip('\n').split("null")[1])

print("number " " instances " " name " " instances " " null " " instances ")
print(" ".join(str(x) for x in number), len(number), " ".join(str(x) for x in name), len(name), " ".join(str(x) for x in null), len(null) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM