I have a program that is reading in a csv file and outputting a number of fields into a text widget, my initial hack of allocating variables works fine, but doesnt give me any flexibility to display more than three lines from the csv file, so i need to go down the route of using a loop routine. Unfortunately I'm unsure how to attack this with what I currently have. My hack code follows below.
def checkcsv():
with open("lesspreadsheettest.csv") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
result=(row['Shop Order'])
if sonumber.get() == result:
descQty=(row['Quantity'])
descInfo=(row['Description'])
descPN=(row['Part Number'])
descDwg1=(row['Drawings1'])
descIss1=(row['Issue1'])
descDwg2=(row['Drawings2'])
descIss2=(row['Issue2'])
descDwg3=(row['Drawings3'])
descIss3=(row['Issue3'])
self.outputQty.insert(1.0, descQty)
self.outputDesc.insert(1.0, descPN, "", ": ", "", descInfo)
self.dwgoutputbox.insert(1.0, descDwg3, "dwg", " Issue: ", "", descIss3, "", "\n")
self.dwgoutputbox.insert(1.0, descDwg2, "dwg", " Issue: ", "", descIss2, "", "\n")
self.dwgoutputbox.insert(1.0, descDwg1, "dwg", " Issue: ", "", descIss1, "", "\n")
self.outputQty.configure(state="disabled")
self.outputDesc.configure(state="disabled")
self.dwgoutputbox.configure(state="disabled")
Hmm, to be honest I feel like one of us is missing something quite easy and obvious. I've tweaked your code a little bit. But all I've done is adding one for loop, you'll. Maybe this is what you are looking for.. If not, hopefully we'll be able to specify the problem better:
def checkcsv():
with open("lesspreadsheettest.csv") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
result=(row['Shop Order'])
if sonumber.get() == result:
descQty=(row['Quantity'])
descInfo=(row['Description'])
descPN=(row['Part Number'])
# I've added the following four lines of code
# and commented out some lines, hopefully it is clear
# how to add more descDwg, descIss, simply change the range
# assuming your csv work that way = DrawingsX, IssueX...
for i in xrange(1,4):
descDwg=(row['Drawings'+ str(i)])
descIss=(row['Issue'+ str(i)])
# check, whether the issue is empty if so, skip to the next one
if descIss == '':
continue
self.dwgoutputbox.insert(1.0, descDwg, "dwg", " Issue: ", "", descIss, "", "\n")
# descDwg1=(row['Drawings1'])
# descIss1=(row['Issue1'])
# descDwg2=(row['Drawings2'])
# descIss2=(row['Issue2'])
# descDwg3=(row['Drawings3'])
# descIss3=(row['Issue3'])
self.outputQty.insert(1.0, descQty)
self.outputDesc.insert(1.0, descPN, "", ": ", "", descInfo)
# self.dwgoutputbox.insert(1.0, descDwg3, "dwg", " Issue: ", "", descIss3, "", "\n")
# self.dwgoutputbox.insert(1.0, descDwg2, "dwg", " Issue: ", "", descIss2, "", "\n")
# self.dwgoutputbox.insert(1.0, descDwg1, "dwg", " Issue: ", "", descIss1, "", "\n")
self.outputQty.configure(state="disabled")
self.outputDesc.configure(state="disabled")
self.dwgoutputbox.configure(state="disabled")
Related
I'm getting following error while escaping special character '&' after running http://localhost:8983/solr/amazon_products/select?q=*:*&fq=Category:"Toys \& Games "
this query in Solr
{
"responseHeader": {
"zkConnected": true,
"status": 400,
"QTime": 0,
"params": {
"q": "*:*",
"Games \"": "",
"fq": "Category:\"Toys \\",
"rows": "70"
}
},
"error": {
"metadata": [
"error-class", "org.apache.solr.common.SolrException",
"root-error-class", "org.apache.solr.parser.TokenMgrError"
],
"msg": "org.apache.solr.search.SyntaxError: Cannot parse 'Category:\"Toys \\': Lexical error at line 1, column 17. Encountered: <EOF> after : \"\\\"Toys \\\\\"",
"code": 400
}}
Category field contains values like below
"Category":["Toys & Games "," Learning & Education "," Science Kits & Toys"]
"Category":["Home & Kitchen "," Home Décor "," Window Treatments "," Window Stickers & Films ", " Window Films"],
And category field is of type string with multivalued=true
{
"name":"Category",
"type":"string",
"multiValued":true,
"stored":true},
How to search properly for Category:"Toys & Games "
NOTE: I tried http://localhost:8983/solr/amazon_products/select?q=*:*&fq=Category:Toys* AND *"Games "&rows=70 this query and it worked fine, but If I excatly want to serach for string 'Toys & Games ' how to do that by properly escaping special character '&'
You'll need to encode some of the characters. For example the following command:
$ curl 'http://localhost:8983/solr/puldata/select?fq=title_t%3A%22Woody%20Herman%20%26%20His%20Orchestra%22&q=*&start=0'
will query fq=title_t:"Woody Herman & His Orchestra". Notice how the :, ", spaces, and the & characters are encoded.
I have a data frame column containing sentences.
Within these sentences, there's the whole host of words which I want to remove.
These are words that could appear more than once in a single sentence, and when found I want to remove these words entirely.
e.g.
Sample list of words for removal: ("the", "and", "a") * (list will have 100's of words)
String Before: "the quick brown fox jumps over the lazy dog and cat"
String After: "quick brown fox jumps over lazy dog cat"
sentences <- as.data.frame(c("it's a new sentence","another sentence i've constructed","and a third sentence"))
colnames(sentences) <- c("sentence")
stop_words <- list( "i" = '', "a" = "", "me" = '' , "my" = "", "myself" = "", "we" = "", "it's" = "", "a" = "", "i've" = "")
stop_pattern <- paste0("\\b", "(", paste0(stop_words, collapse = "|"),")","\\b")
trimws(gsub("\\s{2}", " ", gsub(stop_pattern, "", sentences$sentence)))
Output should remove words such as "I've" from the above sentences, however fails to do so.
Output is as shows:
[1] "it's a new sentence" "another sentence i've constructed" "and a third sentence"
Try:
stop_pattern <- paste0("\\b", "(", paste0(stop_words, collapse = "|"),")","\\b") trimws(gsub("\\s{2}", " ", gsub(stop_pattern, "", sentences)))
def createlist(json, filename, arraygroup):
filehdl = open(filename, "wb")
for key, value in rulegroup.items():
filehdl.write('#' + value + "\n")
for list in json:
if list['arraygroup'] == value:
filehdl.write(list['name'] + " " + list['surname'] + "\n")
filehdl.close()
#depart1
testname testsurname
testname1 testsurname2
testname3 testsurname3
#depart2
#depart3
Json has 5 names, and 2 names should be places in #depart2, #depart3 base on their correct department.
Hi i have here a code that will create a file and separate the names in json file base on there group, but the 2nd forloop (with json) was not resetting its index, so after 1st loop of the forloop list was stuck.
tnx for answer.^^,
I already resolve the problem by inputing json obj to array.
jsonlist = []
for obj in json:
jsonlist.append(obj)
then change the 2nd loop with jsonlist
for list in jsonlist:
if list['arraygroup'] == value:
filehdl.write(list['name'] + " " + list['surname'] + "\n")
I need to sum two fields but I couldn't.
Can you help me?
=iif(Fields!ABC.Value= "", Nothing, Sum(Fields!PA.Value))
=iif(Fields!XYZ.Value= "", Nothing, Sum(Fields!PA.Value))
It does not work that way:
=iif(Fields!ABC.Value= "", Nothing, Sum(Fields!PA.Value)) +
iif(Fields!XYZ.Value= "", Nothing, Sum(Fields!PA.Value))
To sum them I should think you just want to default to 0 rather than Nothing
=iif(Fields!ABC.Value= "", 0, Sum(Fields!PA.Value)) +
iif(Fields!XYZ.Value= "", 0, Sum(Fields!PA.Value))
I think what he is looking for is something like this:
=Sum(IIF(Fields!ABC.Value = "", Nothing, Fields!PA.Value)
=Sum(IIF(Fields!XYZ.Value = "", Nothing, Fields!PA.Value)
I have a small text file that I would like to extract some values using autohotkey.
Example of text file's content:
Date: 2014-12-02 12:06:47
Study: G585.010.411
Image: 6.24
Tlbar: 2.60
Notes: 0.74
My current code:
FileReadLine, datetime, C:\File.txt, 1
datedsp := SubStr(datetime, 7)
Sleep 500
FileReadLine, study, C:\File.txt, 2
studydsp := SubStr(study, 7)
Sleep 500
FileReadLine, image, C:\File.txt, 3
imgdsp := SubStr(image, 7)
Sleep 500
FileReadLine, notes, C:\File.txt, 5
notesdsp := SubStr(notes, 7)
Sleep 500
MsgBox %datedsp%
MsgBox %studydsp%
MsgBox %imgdsp%
MsgBox %notesdsp%
All I want to do is to grab the value of each of those lines and assign it to variables. For example, studydsp value would be G58500411, imagedsp value would be 6.24, datedsp value would be 2014-12-02 12:06:47.
Is there anyway to achieve this in a better way?
Possible issues with this code:
I am unable to get the string from the date line perhaps due to a space at
the beginning(?)
I can't get the SubStr value of either date (refer to 1st issue) or
study (perhaps because of special characters?)
You can use FileRead and RegExMatch
var:="
(
Date: 2014-12-02 12:06:47
Study: G585.010.411
Image: 6.24
Tlbar: 2.60
Notes: 0.74
)"
;~ FileRead, var, C:\file.txt
pos:=1
while pos := RegExMatch(var, "\s?(.*?):(.*?)(\v|\z)", m, pos+StrLen(m))
%m1% := m2
msgbox % "Date holds " date
. "`nStudy holds " Study
. "`nImage holds " Image
. "`nTlbar holds " Tlbar
. "`nNotes holds " Notes
Just remove the var part and uncomment the fileread line, at least thats one way to do it to :)
hope it helps
Basically the same as #blackholyman's answer, but using an object based approach by building a value map:
fileCont =
(
Date: 2014-12-02 12:06:47
Study: G585.010.411
Image: 6.24
Tlbar: 2.60
Notes: 0.74
)
valueMap := {}
; Alternatively, use: Loop, Read, C:\file.txt
Loop, Parse, fileCont, `r`n
{
RegExMatch(A_LoopField, "(.*?):(.*)", parts)
; Optionally make keys always lower case:
; StringLower, parts1, parts1
valueMap[Trim(parts1)] := Trim(parts2)
}
msgbox % "Date = " valueMap["Date"]
. "`nImage = " valueMap["Image"]
; We can also iterate over the map
out := ""
for key, val in valueMap
{
out .= key "`t= " val "`n"
}
msgbox % out