[Solved] how to replace first line of a file using python or other tools?

Using Python 3: import argparse from sys import exit from os.path import getsize # collect command line arguments parser = argparse.ArgumentParser() parser.add_argument(‘-p’, ‘–parameter’, required=True, type=str) parser.add_argument(‘-f’, ‘–file’, required=True, type=str) args = parser.parse_args() # check if file is empty if getsize(args.file) == 0: print(‘Error: %s is empty’ % args.file) exit(1) # collect all lines first lines … Read more

[Solved] How to retrieve data from json response with scrapy?

You’ll want to access: [‘hits’][‘hits’][x][‘_source’][‘apply_url’] Where x is the number of items/nodes under hits. See https://jsoneditoronline.org/#left=cloud.22e871cf105e40a5ba32408f6aa5afeb&right=cloud.e1f56c3bd6824a3692bf3c80285ae727 As you can see, there are 10 items or nodes under hits -> hits. apply_url is under _source for each item. def parse(self, response): jsonresponse = json.loads(response.body_as_unicode()) print(“============================================================================================================================”) for x, node in enumerate(jsonresponse): print(jsonresponse[‘hits’][‘hits’][x][‘_source’][‘apply_url’]) For example, print(jsonresponse[‘hits’][‘hits’][0][‘_source’][‘apply_url’]) would produce: … Read more

[Solved] Append an Auto-Incremented number to the end of a file?

The answers that others have put forwards helped me to create a program that every time the .py file is called it counts the number of files in the sub-directory and adds it to a file name and creates the file The Code I Used: path, dirs, files = next(os.walk(“C:/xampp/htdocs/addfiles/text/”)) file_count = len(files) save_path=”C:/xampp/htdocs/addfiles/text/” name_of_file=”text” … Read more

[Solved] Syntax Error: XPath Is Not a Legal Expression

That’s a lousy diagnostic message. Your particular XPath syntax problem Rather than ||, which is logical OR in some languages, you’re probably looking for |, which is nodeset union in XPath. (That is, assuming you’re not aiming for XPath 3.0’s string concatenation operator.) How to find and fix XPath syntax problems in general Use a … Read more

[Solved] Python delete row in file after reading it

You shouldn’t concern yourself about cpu usage when doing disk IO — disk IO is very slow compared to almost any in-memory/cpu operation. There are two strategies to deleting from the middle of a file: writing all lines to keep to a secondary file, then renaming the secondary file to the original file name. copy … Read more

[Solved] Extracting a determined value from a list

You cannot perform a split directly on the list, you can only perform a split on a string (since the list is already split). So iterate over your list of strings and split each of them. I will shorten the code to make it more readable, just replace it with your list. strings = [‘nbresets:0,totalsec:14,lossevtec:0,lossevt:0’] … Read more

[Solved] Can someone explain what map and lambda does?

lambda -> created new function with parameters following it till : then follows function body map -> takes function and apply it to each element of collection and put returned value from such function into new collection Here you can read more on such a style of programming: https://docs.python.org/2.7/howto/functional.html 0 solved Can someone explain what … Read more

[Solved] why I have negative date by subtraction of two column?

@cᴏʟᴅsᴘᴇᴇᴅ explain it better: When two datetime objects are subtracted, the result is a timedelta. Depending on which date was larger, the result could be positive or negative. Also if all values in column have no times, in pandas are not shown. Patient[“Waiting”] = Patient[“Appointment”] – Patient[“Scheduled”] 2016-04-29 00:00:00 – 2016-04-29 18:38:08 For remove negative … Read more

[Solved] How to hide location of image in django?

Using image = forms.ImageField(widget=forms.FileInput,) in forms class ProfileUpdate(forms.ModelForm): image = forms.ImageField(widget=forms.FileInput,) class Meta: model = UserInfo fields = (‘image’, ‘fullname’, ‘mobile’, ‘occupation’, ‘address’, ‘city’, ‘state’, ‘pincode’) solved How to hide location of image in django?

[Solved] What does >>> do in python

When you see people’s code online, you may see >>> before their code because they ran through it in the terminal. For example: >>> print “Hello, World!” Hello, World! >>> This is code they typed into the python terminal. That means they haven’t saved their code to a file. In the terminal, the arrows appear … Read more

[Solved] Removing Capitalized Strings from Input File in Python3.4?

From the code it would appear that your file is really a csv file (your are reading the file with csv.reader). Assuming that this is the case, the following should work: def filtercaps(infilename,outfilename): import csv list_of_words=[] list=[] with open(infilename) as inputfile: for row in csv.reader(inputfile): list.append(row) for l in list: if len(l) == 0: continue … Read more