Cant print list out of python function
Question:
Not that great at python so I might be making a mistake, but my SplitAgain only prints the first line outside of the function. I did try having a forloop but that didnt work either.
print 'List Local Files'
localFile = open('Local.csv','r')
for line in localFile:
splitLine = line.split(",")
splitAgain = splitLine[9].replace('"', '')
# print splitAgain
print 'Collection Files'
CollectionFile = open('Collection.csv', 'r')
for line in CollectionFile:
sLine = line.split(",")
newArray = sLine[4] + sLine[5]
newArray2 = newArray.replace("/XXXX/",'')
print splitAgain
I would want splitAgain to print all the values outside of the foorloop, as i will be running a diff on SplitAgain and newArray2 in the second stage.
But at this stage it only gives me the first line.
I did try iteration of the list, but it gave me each character on a new line.
I tried this
for i in SplitAgain:
print i;
Answers:
As I understand you are trying to get the value for a column in a csv file and print it. Here a modified version of your code.
print 'List Local Files'
VALUES_SEPARATOR = ","
with open('./local.csv','r') as localFile :
lines = []
for line in localFile:
#use line[:-1] to remove the end line "n" symbol.
splitLine = line[:-1].split(VALUES_SEPARATOR)
splitAgain = splitLine[2].replace('"', '')
lines.append(splitAgain)#store the line in a out-of-the-loop variable.
print(lines)
As you can see a list is used to store the result outside of the loop as @tdelaney suggested it in comments. Applyied on this file :
name, owner,"local file names"
foo,bar,"foo the foo"
foofoo,bar,"bar the bar"
It give the following result :
List Local Files
['local file names', 'foo the foo', 'bar the bar']
Is that satisfying your needs ?
I hope so 😉
Maybe you could try making splitAgain as a global variable.
This is how it should be like:
print ('List Local Files')
localFile = open('Local.csv','r')
for line in localFile:
splitLine = line.split(",")
splitAgain = splitLine[9].replace('"', '')
# print (splitAgain)
print ('Collection Files')
CollectionFile = open('Collection.csv', 'r')
for line in CollectionFile:
sLine = line.split(",")
newArray = sLine[4] + sLine[5]
newArray2 = newArray.replace("/XXXX/",'')
print (splitAgain)
This is python so no need for ;.
Not that great at python so I might be making a mistake, but my SplitAgain only prints the first line outside of the function. I did try having a forloop but that didnt work either.
print 'List Local Files'
localFile = open('Local.csv','r')
for line in localFile:
splitLine = line.split(",")
splitAgain = splitLine[9].replace('"', '')
# print splitAgain
print 'Collection Files'
CollectionFile = open('Collection.csv', 'r')
for line in CollectionFile:
sLine = line.split(",")
newArray = sLine[4] + sLine[5]
newArray2 = newArray.replace("/XXXX/",'')
print splitAgain
I would want splitAgain to print all the values outside of the foorloop, as i will be running a diff on SplitAgain and newArray2 in the second stage.
But at this stage it only gives me the first line.
I did try iteration of the list, but it gave me each character on a new line.
I tried this
for i in SplitAgain:
print i;
As I understand you are trying to get the value for a column in a csv file and print it. Here a modified version of your code.
print 'List Local Files'
VALUES_SEPARATOR = ","
with open('./local.csv','r') as localFile :
lines = []
for line in localFile:
#use line[:-1] to remove the end line "n" symbol.
splitLine = line[:-1].split(VALUES_SEPARATOR)
splitAgain = splitLine[2].replace('"', '')
lines.append(splitAgain)#store the line in a out-of-the-loop variable.
print(lines)
As you can see a list is used to store the result outside of the loop as @tdelaney suggested it in comments. Applyied on this file :
name, owner,"local file names"
foo,bar,"foo the foo"
foofoo,bar,"bar the bar"
It give the following result :
List Local Files
['local file names', 'foo the foo', 'bar the bar']
Is that satisfying your needs ?
I hope so 😉
Maybe you could try making splitAgain as a global variable.
This is how it should be like:
print ('List Local Files')
localFile = open('Local.csv','r')
for line in localFile:
splitLine = line.split(",")
splitAgain = splitLine[9].replace('"', '')
# print (splitAgain)
print ('Collection Files')
CollectionFile = open('Collection.csv', 'r')
for line in CollectionFile:
sLine = line.split(",")
newArray = sLine[4] + sLine[5]
newArray2 = newArray.replace("/XXXX/",'')
print (splitAgain)
This is python so no need for ;.