简体   繁体   English

如何在python中逐行读取文件

[英]how to read file line by line in python

I'm trying to read below text file in python, I'm struggling to get as key value in output but its not working as expected: 我正在尝试在python中读取以下文本文件,我正在努力获取输出中的键值,但其无法正常工作:

test.txt test.txt

productId1 ProdName1,ProdPrice1,ProdDescription1,ProdDate1
productId2 ProdName2,ProdPrice2,ProdDescription2,ProdDate2
productId3 ProdName3,ProdPrice3,ProdDescription3,ProdDate3
productId4 ProdName4,ProdPrice4,ProdDescription4,ProdDate4

myPython.py myPython.py

import sys
with open('test.txt') as f
  lines = list(line.split(' ',1) for line in f)
  for k,v in lines.items();
     print("Key : {0}, Value: {1}".format(k,v))

I'm trying to parse the text file and trying to print key and value separately. 我正在尝试解析文本文件,并尝试分别打印键和值。 Looks like I'm doing something wrong here. 看来我在这里做错了。 Need some help to fix this? 需要一些帮助来解决此问题吗?

Thanks! 谢谢!

You're needlessly storing a list. 您不需要存储列表。

Loop, split and print 循环,拆分和打印

with open('test.txt') as f:
    for line in f:
        k, v = line.rstrip().split(' ',1) 
        print("Key : {0}, Value: {1}".format(k,v))

This should work, with a list comprehension: 这应该可以工作,并且具有列表理解:

with open('test.txt') as f:
    lines = [line.split(' ',1) for line in f]
    for k, v in lines:
        print("Key: {0}, Value: {1}".format(k, v))

You can make a dict right of the bat with a dict comp and than iterate the list to print as you wanted. 您可以使用dict comp在蝙蝠右侧做出dict,然后迭代列表以根据需要进行打印。 What you had done was create a list, which does not have an items() method. 您所做的就是创建一个没有items()方法的列表。

with open('notepad.txt') as f:
    d = {line.split(' ')[0]:line.split(' ')[1] for line in f}
    for k,v in d.items():
        print("Key : {0}, Value: {1}".format(k,v))

lines is a list of lists, so the good way to finish the job is: lines是一个列表列表,因此完成工作的好方法是:

import sys
with open('test.txt') as f:
  lines = list(line.split(' ',1) for line in f)
for k,v in lines:
   print("Key : {0}, Value: {1}".format(k,v))

Perhaps I am reading too much into your description but I see one key, a space and a comma limited name of other fields. 也许我在您的说明中读的太多了,但是我看到其他字段的一个键,一个空格和一个逗号限制的名称。 If I interpret that as their being data for those items that is comma limited then I would conclude you want a dictionary of dictionaries. 如果我将其解释为逗号限制的那些项的数据,那么我会得出结论,您需要词典字典。 That would lead to code like: 这将导致如下代码:

data_keys = 'ProdName', 'ProdPrice', 'ProdDescription', 'ProdDate'
with open('test.txt') as f:
  for line in f:
    id, values = l.strip().split()  # automatically on white space
    keyed_values = zip(data_keys, values.split(','))
    print(dict([('key', id)] + keyed_values))

You can use the f.readlines() function that returns a list of lines in the file f. 您可以使用f.readlines()函数返回文件f中的行列表。 I changed the code to include f.lines in line 3. 我将代码更改为在第3行中包含f.lines。

import sys
with open('test.txt') as f:
  lines = list(line.split(' ',1) for line in f.readlines())
  for k,v in lines.items();
     print("Key : {0}, Value: {1}".format(k,v))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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