Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need to make a command line application which has two parameters:
the location of the input file, and
the location of the output file.
The input file is a GTFS (.txt) file.
The output file is a .shp file.
How should I do this?
To get command line parameters:
% python3 your_script.py parameter1 parameter2
Where parameter1 and parameter2 are you input file and output file names:
import sys
parameters = sys.argv
#parameters now contains 3 strings:
#"your_script.py" in parameters[0]
#"parameter1" in parameters [1]
#"parameter2" in parameters [2]
So you can use the command line arguments as variables. To open the files:
in_file = open(parameters[1] + ".txt") #from your problem statement, it sounds
#like your filenames don't include extensions. If they do, remove
#the (+ ".txt") part
out_file = open(parameters[2] + ".shp", 'w')
For more information about I/O operations in Python, see this link
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am new to python and I am using idle 3.7.4. I want to automate command line to input() in python.
For example:
a = input("Please enter your name : ")
I want to add my name without manually typing it. I read about run() method under subprocess module, but I am not sure how to implement it.
Any helps are highly appreciated. Thanks.
you can use the mock library to do this with the builtins. For example, like this:
import mock
import builtins
def input_test():
ans = input("Please input your name :")
return f"your name is {ans}"
def test_input():
with mock.patch.object(builtins, 'input', lambda _: "okay"):
assert input_test() == 'your name is okay'
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a folder that contains zip files in subfolders. I want to unzip all the files using this python code. code shows no error but the files are not extracted can't figure out the problem. Thanks in Advance.
from zipfile import ZipFile
from pathlib import Path
entries = Path('E:\\Bootcamp')
for entry in entries.rglob('*.zip'):
with ZipFile(entry, 'r') as zip:
print('Check1')
zip.extractall()
print('check2')
The extracted files will be located in the folder where your python file has been saved
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How can I get all the Request URL in chrome network
Right click -> Copy -> Copy all as HAR
Then you can import it like this:
import json
obj = json.loads(
'''
<paste here>
'''
)
You can then get urls = [ entry['request']['url'] for entry in obj['log']['entries'] ]
You may need to replace \" with \\" in your text editor for it to compile.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm using python and am trying to create a new csv file using the csv module (i.e. one that doesn't currently exist). Does anyone know how to do this?
Thanks in advance,
Max
if you want simply create a csv file you can use built in method open, for more about open check this
with open("filename.csv","a+") as f:
f.write(...)
or if you want to read an exist csv file you can use this
import csv
with open('filename.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in
print ', '.join(row)
#if you want to save the file into given path
import os
os.rename("filename.csv","path/to/new/desination/for/filename.csv")
for more check docs
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
The download link I want to manipulate is below:
http://hfrnet.ucsd.edu/thredds/ncss/grid/HFR/USWC/6km/hourly/RTV/HFRADAR,_US_West_Coast,_6km_Resolution,_Hourly_RTV_best.ncd?var=u&var=v&north=47.20&west=-126.3600&east=-123.8055&south=37.2500&horizStride=1&time_start=2015-11-01T00%3A00%3A00Z&time_end=2015-11-03T14%3A00%3A00Z&timeStride=1&addLatLon=true&accept=netcdf
I want to make anything that's in bold a variable, so I can ask the user what coordinates and data set they want. This way I can download different data sets by using this script. I would also like to use the same variables to name the new file that was downloaded ex:USWC6km20151101-20151103.
I did some research and learned that I can use the urllib.parse and urllib2, but when I try experimenting with them, it says "no module named urllib.parse."
I can use the webbrowser.open() to download the file, but manipulating the url is giving me problems
THANK YOU!!
Instead of urllib you can use requests module that makes downloading content much easier. The part that makes actual work is just 4 lines long.
# first install this module
import requests
# parameters to change
location = {
'part': 'USWC',
'part2': '_US_West_Coast',
'km': '6km',
'north': '45.0000',
'west': '-120.0000',
'east': '-119.5000',
'south': '44.5000',
'start': '2016-10-01',
'end': '2016-10-02'
}
# this is template for .format() method to generate links (very naive method)
link_template = "http://hfrnet.ucsd.edu/thredds/ncss/grid/HFR/{part}/{km}/hourly/RTV/\
HFRADAR,{part2},_{km}_Resolution,_Hourly_RTV_best.ncd?var=u&var=v&\
north={north}&west={west}&east={east}&south={south}&horizStride=1&\
time_start={start}T00:00:00Z&time_end={end}T16:00:00Z&timeStride=1&addLatLon=true&accept=netcdf"
# some debug info
link = link_template.format(**location)
file_name = location['part'] + location['km'] + location['start'].replace('-', '') + '-' + location['end'].replace('-', '')
print("Link: ", link)
print("Filename: ", file_name)
# try to open webpage
response = requests.get(link)
if response.ok:
# open file for writing in binary mode
with open(file_name, mode='wb') as file_out:
# write response to file
file_out.write(response.content)
Probably the next step would be running this in loop on list that contains location dicts. Or maybe reading locations from csv file.