I have a CSV file with 11 columns. I am new to Python and trying to make a code that sorted column 2 alphabetically (string) and then prints the rows of the sorted list.
I only want the rows printed where column 1 == 1.
If column 1 == 0 then i want the script to ignore this row and continue on to the next line without printing it.
in_file = open('filename.csv', 'r')
dict = {}
#should print all rows where field [0] == 1
print('List of rows where column one is equal to zero in alphabetical order of column 2')
for line in in_file:
line = line.strip('\n')
fields = line.split(',')
c1 = fields[0]
c2 = fields[1]
c3 = fields[2]
c4 = fields[3]
c5 = fields[4]
c6 = fields[5]
c7 = fields[6]
c8 = fields[7]
c9 = fields[8]
c10 = fields[9]
c11 = fields[10]
if c2 not in dict:
dict[c2] = c1
if c3 not in dict:
dict[c3] = c1
if c4 not in dict:
dict[c4] = c1
if c5 not in dict:
dict[c5] = c1
if c6 not in dict:
dict[c6] = c1
if c7 not in dict:
dict[c7] = c1
if c8 not in dict:
dict[c8] = c1
if c9 not in dict:
dict[c9] = c1
if c10 not in dict:
dict[c10] = c1
if c11 not in dict:
dict[c11] = c1
name = dict.keys()
sorted_names = sorted(name)
for name in sorted_names:
c1 = dict[name]
rows = [row for row in in_file if row ['c1']!= 1]
for row in rows:
print(c3, c2, c4, c5)
in_file.close()
As you commented, the error pops up in the 'rows = [row for row in in_file if row ['c1']!= 1]' line. As far as i know, the error is really simple:
you input indices as strings, but they Always have to be integers, as the error says. for example: your index is ['c1'], but c1 is a variable. using [c1] would probably solve the problem.
Related
Following this post Google Sheets QUERY with WHERE on multiple columns I build up my formula to select specific values from more columns but when I had a condition data start to be confused and not equal to what I selected. I started from this query which works perfectly on one column for each IF:
=QUERY(Concerti!A1:AL; "SELECT * WHERE 1=1" &IF(A2="TUTTI";"";" AND E = '"&A2&"' ") &IF(E2="TUTTI";"";" AND H = '"&E2&"' ") &IF(F2="TUTTI";"";" AND B = "&F2&" "); 1)
Then I wrote this query to select (and filter) the content in A4, E4 and F4 but it doesn't work correctly i.e. it keeps some data which doesn't exactly match with the values on A4, E4 and F4.
=QUERY(Concerti!A1:AL; "SELECT * WHERE 1=1" &IF(A2="TUTTI";"";" AND E = '"&A2&"' ") &IF(E2="TUTTI";"";" AND H = '"&E2&"' ") &IF(F2="TUTTI";"";" AND B = "&F2&" ") &IF(A4="TUTTI";"";" AND I = ('"&A$4&"') OR (K='"&A$4&"' OR M='"&A$4&"' OR O='"&A$4&"' OR Q='"&A$4&"' OR S='"&A$4&"') ") &IF(E4="TUTTI";"";" AND J = ('"&E$4&"') OR (L='"&E$4&"' OR N='"&E$4&"' OR P='"&E$4&"' OR R='"&E$4&"' OR T='"&E$4&"') ") &IF(F4="TUTTI";"";" AND Y = ('"&F$4&"') OR (AA='"&F$4&"' OR AC='"&F$4&"' OR AE='"&F$4&"' OR AG='"&F$4&"' OR AI='"&E$4&"') "); 1)
Where is my mistake? Thank you so much in advance!!! I post a screenshot of the project:
[![Filtering mask](https://i.stack.imgur.com/cRFLv.png)](https://i.stack.imgur.com/cRFLv.png)
try:
=QUERY(Concerti!A1:AL; "where 1=1"&
IF(A2="TUTTI";;" and E = '"&A2&"'")&
IF(E2="TUTTI";;" and H = '"&E2&"'")&
IF(F2="TUTTI";;" and B = "&F2&" ")&
IF(A4="TUTTI";;" and (I = '"&A$4&"'
or K = '"&A$4&"'
or M = '"&A$4&"'
or O = '"&A$4&"'
or Q = '"&A$4&"'
or S = '"&A$4&"')")&
IF(E4="TUTTI";;" and (J = '"&E$4&"'
or L = '"&E$4&"'
or N = '"&E$4&"'
or P = '"&E$4&"'
or R = '"&E$4&"'
or T = '"&E$4&"')")&
IF(F4="TUTTI";;" and (Y = '"&F$4&"'
or AA= '"&F$4&"'
or AC= '"&F$4&"'
or AE= '"&F$4&"'
or AG= '"&F$4&"'
or AI= '"&E$4&"')"); 1)
I want to import text files using a loop instead of importing them one by one.
I have 9 text files labelled clump0.txt - clump9.txt and after importing them I have to assign a variable.
ymport.textClumps('/tmp/clump1.txt')
#c0 = getClumpInfo()
c0 = pack.SpherePack()
c0.fromSimulation()
O.resetThisScene()
ymport.textClumps('/tmp/clump1.txt')
#c1 = getClumpInfo()
c1 = pack.SpherePack()
c1.fromSimulation()
O.resetThisScene()
ymport.textClumps('/tmp/clump2.txt')
#c1 = getClumpInfo()
c2 = pack.SpherePack()
c2.fromSimulation()
O.resetThisScene()
ymport.textClumps('/tmp/clump3.txt')
#c1 = getClumpInfo()
c3 = pack.SpherePack()
c3.fromSimulation()
O.resetThisScene()
...
Later I am using c0-c9 in this:
test = sp.makeClumpCloud(minCorner, maxCorner, [c0,c1,c2,c3,c4,c5,c6,c7,c8,c9], periodic=True, num = -1)
I already tried:
# load clumps
arr = [c1, c2, c3, c4, c5, c6, c7, c8, c9]
for i in range (0, 9):
ymport.textClumps("/tmp/clump{}.txt".format(i))
arr[i] =pack.SpherePack()
arr[i].fromSimulation()
O.resetThisScene()
Ok I solved it doing
load clumps
arr = [0,0,0,0,0,0,0,0,0]
for i in range (0, 9):
ymport.textClumps(str(os.getcwd()) + "/tmp/clump{}.txt".format(i))
arr[i] = pack.SpherePack()
arr[i].fromSimulation()
O.resetThisScene()
I just had to assign a placeholder into the array :) Thanks
I am trying to convert CSV file contents from format A to Format B. I tried pandas, default dict, Dict writer, etc but I could not make it out.The problem is that it is printing horizontally but not vertically. Please find the example below.
Format A:
item meas COL A COL B COL C COL D
84P37W265B3 B1 3970 99.82368 99.82368 0.07556675
84P37W265B3 B3 3960 95.10101
84P37W265B3 B5 3705 96.89609 96.89609 0.05398111
84P37W265B3 B6 3763 98.45868 98.45868 0.02657454
84P3XT135A4 B1 7904 99.73431 99.73431 0.02
84P3XT135A4 B3 7817 97.5694 100 0.01
Format B:
item 84P37W265B3 84P3XT135A4
meas B1 B1
COL A 3970 7904
COL B 99.82368 99.73431
COL C 99.82368 99.73431
COL D 0.07556675 0.02
meas B3 B3
COL A 3960 7817
COL B 95.10101 97.5694
COL C - 100
COL D - 0.01
meas B5 -
COL A 3705 -
COL B 96.89609 -
COL C 96.89609 -
COL D 0.05398111 -
meas B6 -
COL A 3763 -
COL B 98.45868 -
COL C 98.45868 -
COL D 0.02657454 -
Can anyone help me out in this, Thanks in advance...
you can use python csv.
here is the working code for conversion of Format A to Format B
import csv
a_csv = r'D:\A.csv'
b_csv = r'D:\B.csv'
# Read Format A CSV Format
with open(a_csv) as csvfile:
reader = csv.DictReader(csvfile)
item_list = []
item_list.insert(0, 'Item')
meas_list = []
meas_list.insert(0, 'meas')
cola_list = []
cola_list.insert(0, 'COL A')
colb_list = []
colb_list.insert(0, 'COL B')
colc_list = []
colc_list.insert(0, 'COL C')
cold_list = []
cold_list.insert(0, 'COL D')
for row in reader:
item_list.append(row['item'])
meas_list.append(row['meas'])
cola_list.append(row['COL A'])
colb_list.append(row['COL B'])
colc_list.append(row['COL C'])
cold_list.append(row['COL D'])
# Write Format B CSV Format
with open(b_csv, 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(item_list)
writer.writerow(meas_list)
writer.writerow(cola_list)
writer.writerow(colb_list)
writer.writerow(colc_list)
writer.writerow(cold_list)
check output:
Item 84P37W265B3 84P37W265B3 84P37W265B3 84P37W265B3 84P3XT135A4
meas B1 B3 B5 B6 B3
COL A 3970 3960 3705 3763 7817
COL B 99.82368 95.10101 96.89609 98.45868 97.5694
COL C 99.82368 96.89609 98.45868 100
COL D 0.07556675 0.05398111 0.02657454 0.01
I would like to thank #cyclops for the reply. Please find my code for the dynamic type .i.e. the user do not know number of columns in the input csv file.
CODE:
import csv
from collections import defaultdict
column_header=[]
columns = defaultdict(list)
with open('C:\outfile4.csv') as f:
reader = csv.DictReader(f)
for row in reader:
for (k,v) in row.items():
columns[k].append(v)
b_csv = r'C:\outfile5.csv'
with open(b_csv, 'w') as csvfile:
writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for key,values in sorted(columns.iteritems()):
if key == 'item':
writer.writerow([key]+values)
for key,values in sorted(columns.iteritems()):
if key == 'meas':
writer.writerow([key]+values)
for key,values in sorted(columns.iteritems()):
if key != 'item' and key != 'meas':
writer.writerow([key]+values)
It is working but please let me is any other simple way to do this. Thanks in advance.
I have got hex values as a85b080040010000. I want it to be as a8 5b 08 00 40 01 00 00. I have done it by using below code. But I have to work with very large data. So I want computed time to be very low.
import binascii
import re
filename = 'calc.exe'
with open(filename, 'rb') as f:
content = f.readline()
text = binascii.hexlify(content)
text1 = binascii.unhexlify(text)
length1 = 32
length2 = 16
list = re.findall('.{%d}' % length1, text)
list1 = re.findall('.{%d}' % length2, text1)
d = []
for i in range (0, len(list), 1):
temp = ""
l = re.findall('.{%d}' % length2, list[i])
s = l[0]
t = iter(s)
temp += str(' '.join(a+b for a,b in zip(t, t)))
temp += " "
s = l[1]
t = iter(s)
temp += str(' '.join(a+b for a,b in zip(t, t)))
temp += " | " + list1[i]
print temp
You can simply do
x="a85b080040010000"
print re.sub(r"(.{2})",r"\1 ",x)
or
x="a85b080040010000"
print " ".join([i for i in re.split(r"(.{2})",x) if i])
def trav(r,c):
#print("(",r,",",c,")")
ch = a[r+1][c+1]
lu= a[r][c]
#print(lu)
u = a[r][c+1]
#print(u)
ru= a[r][c+2]
#print(ru)
l = a[r+1][c]
#print(l)
r = a[r+1][c+2]
#print(r)
ld= a[r+2][c]
#print(ld)
d = a[r+2][c+1]
#print(d)
rd= a[r+2][c+2]
#print(rd)
a1 = a2 = a3 = a4 = a5 = a6 = a7 = a8 = 0
if(ch == lu-1):
a1 = trav(r-1,c-1)
if(ch == u-1):
a2 = trav(r-1,c)
if(ch == ru-1):
a3 = trav(r-1,c+1)
if(ch == l-1):
a4 = trav(r,c-1)
if(ch == r-1):
a5 = trav(r,c+1)
if(ch == ld-1):
a6 = trav(r+1,c-1)
if(ch == d-1):
a7 = trav(r+1,c)
if(ch == rd-1):
a8 = trav(r+1,c+1)
return max(a1,a2,a3,a4,a5,a6,a7,a8) + 1
while(1):
row,col = [int(x) for x in input().split(" ")]
if(row == 0):
break
a = []
for index in range(10):
a.append([])
for jindex in range(10):
a[index].append('a')
b = []
for index in range(row):
str = input()
for jindex in range(col):
a[index+1][jindex + 1] = ord(str[jindex])
if(str[jindex] == 'A'):
b.append([index,jindex])
#print (a)
#print (b)
ans = max([trav(x[0],x[1]) for x in b])
print(ans)
The code is generating the following error
Traceback (most recent call last): File "C:/Users/DELL/Desktop/ABCPATH.py", line 80, in <module>
ans = max([trav(x[0],x[1]) for x in b]) File "C:/Users/DELL/Desktop/ABCPATH.py", line 80, in <listcomp>
ans = max([trav(x[0],x[1]) for x in b]) File "C:/Users/DELL/Desktop/ABCPATH.py", line 17, in trav
ld= a[r+2][c] IndexError: list index out of range
But according to me it should not, and as I am extremely new to python , I am not able to debug it. Please help
sample input taken:
4 3
ABC
CFG
BDH
ABC
It has to break
r = a[r+1][c+2]
you're reassigning the value of 'r' in that line. please also note that you shouldn't name your string "str" as it is the name of the python string module and you won't be able to use it.