用Python编写一个文本文件(writing item in a text file in python)
我有一个程序可以执行以下操作。
- 你输入一个输入
python 1.txt apple 5 python 1.txt orange 18
- 它会在文本文件中写入项目和数量作为修改后的文本文件的示例
apple : 5 orange : 18
- 它也检查重复,然后添加数量与现有的金额,如果存在像 -
python 1.txt apple 5
apple : 10 orange : 18
问题:如果该项目不存在于文本文件中或未被复制,请将该项目添加到文本文件中。 目前这不起作用
这是我写的 -
import sys def maj(texte, item, nombre): with open(texte, "r") as f_in: lecture = f_in.readlines() with open(texte, "w") as ouvrir: for line in lecture: line = line.strip("\n") mot, chiffre = line.split(" : ")[0].strip(" "), int(line.split(" : ")[1].strip(" ")) if mot == item: ouvrir.write("%s : %s" "\n" % (item, str(chiffre + int(nombre)))) ouvrir.close() else: ouvrir.write("%s : %s" "\n" % (mot, chiffre)) ouvrir.close() def main(): entree = sys.argv[1] item = sys.argv[2] nombre = sys.argv[3] choix = str(entree) texte = choix.strip("[']") maj(texte, item, nombre) if __name__ == "__main__": main()
I have a program that does the following.
- you enter an input
python 1.txt apple 5 python 1.txt orange 18
- it will write the item and amount in the text file an example of the modified text file
apple : 5 orange : 18
- it is also checking for duplicate then add the amount with the existing amount if present like-
python 1.txt apple 5
apple : 10 orange : 18
Problem: if the item doesn't exist in the text file or is not duplicated, add the item in the text file. At the moment this doesn't work
Here is what I have written-
import sys def maj(texte, item, nombre): with open(texte, "r") as f_in: lecture = f_in.readlines() with open(texte, "w") as ouvrir: for line in lecture: line = line.strip("\n") mot, chiffre = line.split(" : ")[0].strip(" "), int(line.split(" : ")[1].strip(" ")) if mot == item: ouvrir.write("%s : %s" "\n" % (item, str(chiffre + int(nombre)))) ouvrir.close() else: ouvrir.write("%s : %s" "\n" % (mot, chiffre)) ouvrir.close() def main(): entree = sys.argv[1] item = sys.argv[2] nombre = sys.argv[3] choix = str(entree) texte = choix.strip("[']") maj(texte, item, nombre) if __name__ == "__main__": main()
原文:https://stackoverflow.com/questions/43672021
更新时间:2023-10-28 12:10
最满意答案
首先检查页面中是否有那个coockie。 如果为true,则获取coockie值并添加到其中1.否则,将此coocike第一次设置为值1:
count = parseInt(Cookies.get('counter')); if(count){ Cookies.set('counter', count + 1); } else{ Cookies.set('counter', 1); }
First check if have that coockie in the page. If true, get the coockie value and add to it 1. Else, set this coocike first time in value 1:
count = parseInt(Cookies.get('counter')); if(count){ Cookies.set('counter', count + 1); } else{ Cookies.set('counter', 1); }
相关问答
更多-
使用$_SESSION (如你所说) 第1页 - 在会话中存储当前时间(或任何时间)(cookie) $_SESSION['startTime'] = time(); 第2页 - 在我们通过cookie的时间(在第1页创建)减去当前时间(意思是访问第2页的时间) if (is_int($_SESSION['startTime'])){ // if the cookie exists: $difference = time() - $_SESSION['startTime']; } else { / ...
-
如果你想要count用户登录的次数,那么通过command-line IMO就不可能....特别是因为有sudo选项假装其他用户可能会失败你的逻辑 - 如果你有任何 此外,有how many times需要有一个时间段( from when when )..所以你需要一个上限时间段,你要从中追踪计数。所有这将是一个混乱理想只是为了保持count 如果我必须跟踪它,我最好的猜测是拥有一个script并维护一个db-table保持登录users的count ! If you want count of how ...
-
您正在使用$user ,但需要$USER 。 喜欢, count=$(who | grep $USER | wc -l) echo "$USER is logged on $count times." 我明白了 efrisch is logged on 4 times. 另一种选择是printf类的 printf "%s is logged on %s times.\n" $USER $(who | grep $USER | wc -l) 为了相同的结果。 You are using $user, b ...
-
默认情况下,当您使用.joins时,Rails使用inner join .joins 。 这就是为什么您没有看到没有相关印象的位置的原因。 您需要告诉它使用left join ,可能是这样的: @user.locations. joins("left join impressions on impressions.impressionable_id = locations.id and impressions.impressionable_type = 'Location'"). select(" ...
-
-
PHP Count登录用户(PHP Count Logged-In Users)[2023-03-12]
您应该添加LastTimeSeen列,而不是IsLoggedIn列。 任何时候一个人访问一个页面,你都会更新该列: UPDATE members SET LastTimeSeen = NOW() WHERE id = $the_user_id 然后,在任何特定时刻使用查询来获取网站上有多少人: SELECT COUNT(*) FROM members WHERE LastTimeSeen > DATE_SUB(NOW(), INTERVAL 5 MINUTE) 这表明有多少人在过去的5分钟内浏览了一个页 ... -
jquery库,用于计算用户登录页面的次数(jquery library to count how many times a user has logged into my page)[2023-06-10]
首先检查页面中是否有那个coockie。 如果为true,则获取coockie值并添加到其中1.否则,将此coocike第一次设置为值1: count = parseInt(Cookies.get('counter')); if(count){ Cookies.set('counter', count + 1); } else{ Cookies.set('counter', 1); } First check if have that coockie in the page. If tru ... -
我认为您的username或password为空,请检查这些值 您可以在jsoup.org下载jsoup的源代码,然后在HttpConnection.java中找到以下代码 public KeyVal value(String value) { Validate.notNull(value, "Data value must not be null"); this.value = value; return this; } I think your ...
-
如果用户通过firebase登录,如何显示不同的页面(How to show different page if user is logged in via firebase)[2022-11-21]
用户端和服务器端是完全不同的执行区域。 因此,正如您可能猜到的那样,如果在客户端上进行身份验证,则调用服务器上的firebase.auth().currentUser是行不通的。 除非客户告诉他,否则服务器进程就没有这些信息。 你可以只有一个请求标题告诉“我被记录为XXX”,但它不安全,因为服务器无法验证该信息,恶意用户可能假装是另一个。 在您的使用案例中,唯一的解决方案是向服务器提供Firebase令牌,然后服务器需要针对firebase服务器验证此令牌,然后才会100%确定客户端身份验证。 我需要在我的 ... -
我这样做有点不同。 在我的config / routes.rb中我只是平常 root 'pages#index' 然后我将app / controllers / pages_controller.rb中的 索引方法更改为 def index if logged_user? render 'user_objects/index' end end 如果用户登录了logged_user,我会说所有这些逻辑? 方法如 def logged_user? !session[:user_id]. ...