【问题标题】:Python: Select particular characters from a file and add them to a new variablePython:从文件中选择特定字符并将它们添加到新变量中
【发布时间】:2017-02-25 17:37:05
【问题描述】:

我对 python 还很陌生,我不知道为什么我做错了。

numberOfOrders = 0
numberOfProducts = 0

allOrders = open("file.txt", "r") #A .txt file in the same directory as the .py file.
#file.txt: 
#(A->[a:20,a:20,b:10,c:25,c:25])
#(B->[d:100,e:70])
#(C->[f:10000,g:200000])

while True:
        theline = allOrders.readline()
        for theline in allOrders:
            for char in theline: #Iterate over each character of a line.
                listProducts = "" #Empty string, will be the concatenation of the wanted characters.
                if char == "[": #Wanted character.
                    listProducts = listProducts + "["
                elif char == ":": #To keep count of no. of products in a list.
                    numberOfProducts += 1
                elif is_number(char) == True: #Function that checks whether char is a number.
                    listProducts = listProducts + str(char) #Add to the string "listProducts".
                elif char == ",": #Wanted character.
                    listProducts = listProducts + str(char)
                elif char == "]":#Wanted character, to end the string.
                    listProducts = listProducts +str(char)
                    break
            numberOfOrders += 1 #To keep track of no. of orders. Each line of file is an order. 
        if len(theline) == 0:
            break

    allOrders.close()

    print(numberOfProducts)
    print(numberOfOrders)
    print(listProducts)

我基本上只想要括号内的数字和逗号。这是我在这里最大的问题。 我得到的输出

 print(listProducts)

 ]

谢谢。

【问题讨论】:

  • 你可以先去掉“for theline in allOrders:”这行似乎不合适...
  • 您的文本文件是否与您在 cmets 中显示的一样完全? IE。是否包含“#(A->”和右括号?
  • 添加您希望输出的样子。第一行是20,20,10,25,25吗?
  • 文本文件包含任何“#”,但包含所有其余部分。输出类似于:[20, 20, 10, 25, 25]。
  • 你是指python列表还是有点像python列表的字符串?字符串转换为整数?看起来您真正想要的是订单和产品的数量。我要大胆猜测。

标签: python file python-3.x if-statement for-loop


【解决方案1】:

关于您的代码,解决方案是:

  1. 删除不连贯的for theline in allOrders
  2. listProducts的初始化移到while循环之前

当然,这可以使用正则表达式进行广泛优化,例如 @tdelaney 所建议的。

【讨论】:

    【解决方案2】:

    您可以通过使用正则表达式去除其他所有内容来保留数字和逗号。然后你有一个小数和逗号字符串,你可以将它们拆分为每个订单行中的产品列表。

    import re
    
    with open('file.txt') as all_orders:
        # substitute '' for all non-digit non-comma then split
        orders = [re.sub(r'[^\d,]', '', line).split(',')
            for line in all_orders]
    
    number_of_orders = len(orders)
    number_of_products = sum(map(len, orders))
    print('orders', number_of_orders, 'products', number_of_products)
    

    【讨论】:

      猜你喜欢
      • 2021-12-21
      • 1970-01-01
      • 2021-01-10
      • 2022-01-21
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      相关资源
      最近更新 更多